IOS AVPlayer won't play video
Thanks s1mon and matt for your help.
Here's the new code it might help someone:
- (void) viewDidAppear:(BOOL)animated
{
avAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:filePath]];
avPlayerItem =[[AVPlayerItem alloc]initWithAsset:avAsset];
avPlayer = [[AVPlayer alloc]initWithPlayerItem:avPlayerItem];
avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:avPlayer];
[avPlayerLayer setFrame:self.view.frame];
[self.view.layer addSublayer:avPlayerLayer];
//[avPlayerLayer setBackgroundColor:[[UIColor redColor]CGColor]];
[avPlayer seekToTime:kCMTimeZero];
[avPlayer play];
}
Thank you.
This might not be the actual problem, but this code is certainly wrong:
[NSURL URLWithString:filePath]
First of all, try not to start with a file path; start with a file URL. That way, there is no conversion needed. But if you must perform a conversion from a file path to a URL, use fileURLWithPath:isDirectory:
. A file URL is a special kind of animal and you must ask for one specifically when you are converting from a path.
I think you forgot to create an instance of AVPlayerItem
in this line:
avPlayerItem =[avPlayerItem initWithAsset:avAsset];
This just sends initWithAsset:
to avPlayerItem
(which is probably nil when this method is called). Try changing it to instantiate an new AVPlayerItem
which you can then assign to avPlayerItem
, like this:
avPlayerItem = [[AVPlayerItem alloc] initWithAsset:avAsset];