Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayer Video Blank but Hear Sound

I'm switching from MPMoviePlayerController to AVPlayer as I need finer grained control over video swapping. The .mov file I was playing with MPMoviePlayerController played fine, but after switching to AVPlayer I hear the audio from the video, but the video just shows the view background that I added the AVPlayerLayer to. Here's how I'm initializing the AVPlayer.

self.player = [[AVPlayer alloc] initWithURL:video];

AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.frame = self.playerContainer.bounds;
[self.playerContainer.layer addSublayer:playerLayer];

Then later I just issue a.

[self.player play];

When the video plays I hear the audio, but see no video. I also tried setting the zPosition to no luck.

playerLayer.zPosition = 1;
like image 945
Gary Rudolph Avatar asked May 28 '13 22:05

Gary Rudolph


2 Answers

Found out it was a result of using AutoLayout. In the viewDidLoad the self.playerContainer.bounds is a CGRectZero.

I had to assign the playerLayer frame in the viewDidAppear to match the playerContainer.

like image 174
Gary Rudolph Avatar answered Oct 21 '22 03:10

Gary Rudolph


Since we use AVPlayerLayer (a subclass of CALayer), we need to set the frame

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    self.avPlayerLayer.frame = self.movieContainerView.bounds;
}
like image 1
onmyway133 Avatar answered Oct 21 '22 03:10

onmyway133