15

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;
Venk
  • 5,949
  • 9
  • 41
  • 52
Gary Rudolph
  • 1,062
  • 9
  • 21

3 Answers3

8

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.

Venk
  • 5,949
  • 9
  • 41
  • 52
Gary Rudolph
  • 1,062
  • 9
  • 21
  • 1
    I don't use autolayout but have the same issue. http://stackoverflow.com/questions/16872193/avqueueplayer-shows-black-screen-when-called-from-app-delegate How could you solve it? – Burak Jun 01 '13 at 12:03
  • For me the video was a CGRectZero and not visible. It was actually playing, just in a 0.0x0.0 screen. – Gary Rudolph Jul 22 '13 at 06:17
  • @Gary I have exact same problem but with a MacOS project - I hear sound but no video just black window - here is my total code: `let fileURL = URL(string: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4") let avAsset = AVURLAsset(url: fileURL!, options: nil) let playerItem = AVPlayerItem(asset: avAsset) videoPlayer = AVPlayer(playerItem: playerItem) let playerView = self.playerView playerView?.player = videoPlayer videoPlayer.play()` Any idea what I need to add to this to get Video to show? – Neal Davis Nov 11 '17 at 16:44
1

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

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    self.avPlayerLayer.frame = self.movieContainerView.bounds;
}
onmyway133
  • 45,645
  • 31
  • 257
  • 263
0

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()
}
Don Park
  • 379
  • 1
  • 5
  • 3