Gary Rudolph
Gary Rudolph

Reputation: 1102

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;

Upvotes: 15

Views: 9141

Answers (3)

Don Park
Don Park

Reputation: 479

Make sure you the statement that plays the video:

[self.player play]

is invoked from main dispatch queue like this (Swift 3):

DispatchQueue.main.async {
    self.player.play()
}

Upvotes: 0

onmyway133
onmyway133

Reputation: 48055

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

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    self.avPlayerLayer.frame = self.movieContainerView.bounds;
}

Upvotes: 1

Gary Rudolph
Gary Rudolph

Reputation: 1102

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.

Upvotes: 8

Related Questions