AVPlayer Video Blank but Hear Sound
Asked Answered
I

3

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;
Intendancy answered 28/5, 2013 at 22:37 Comment(0)
I
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.

Intendancy answered 28/5, 2013 at 23:27 Comment(3)
I don't use autolayout but have the same issue. #16872693 How could you solve it?Quagga
For me the video was a CGRectZero and not visible. It was actually playing, just in a 0.0x0.0 screen.Intendancy
@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?Primula
U
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;
}
Underhand answered 25/8, 2015 at 8:15 Comment(0)
T
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()
}
Torry answered 28/10, 2016 at 1:9 Comment(1)
Sorry it didn't help me out.Picturize

© 2022 - 2024 — McMap. All rights reserved.