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;
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.
Since we use AVPlayerLayer (a subclass of CALayer), we need to set the frame
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.avPlayerLayer.frame = self.movieContainerView.bounds;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With