Ashraf Hussein Ashraf Hussein - 3 months ago 38
iOS Question

IOS AVPlayer won't play video

I'm trying to play a video usingso I got some code from a tutorial but it won't work.

Here's the code I use :

- (void) viewDidAppear:(BOOL)animated
{
avAsset = [AVAsset assetWithURL:[NSURL URLWithString:filePath]];
avPlayerItem =[avPlayerItem initWithAsset:avAsset];
avPlayer = [AVPlayer playerWithPlayerItem:avPlayerItem];
avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
[avPlayerLayer setFrame:self.view.frame];
[self.view.layer addSublayer:avPlayerLayer];
[avPlayer seekToTime:kCMTimeZero];
[avPlayer play];

}


I don't know what's wrong it doesn't give ny errors it just gives black screen.
Thanks.

Answer

Hey guys 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];

}

THXXXXXXXX guys :D

Comments