5

Im trying to display a local recorded video in a AVPlayerLayer which works sometimes. I can hear the audio from the recorded video but can't see the video. Sometimes both video and audio is working, sometimes only audio.

I've tried both with a AVPlayerLayer and AVPlayerViewController but the same issue occurs in both cases. So it's not because of the frames being wrong.

Example code AVPlayerViewController:

let player = AVPlayer(url: url)
let playerController = AVPlayerViewController()
playerController.player = player

self.present(playerController, animated: true) {
  player.play()
}

Example code AVPlayerLayer:

let asset = AVAsset(url: url)
let item = AVPlayerItem(asset: asset)

self.player = AVPlayer(playerItem: item)
self.player?.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions(), context: nil)
self.playerLayer = AVPlayerLayer(player: self.player!)
self.playerLayer?.frame = imageView.bounds
imageView.layer.addSublayer(self.playerLayer!)

Edit 1:

When observing the status of the player, error is nil and the status is readyToPlay

Edit 2:

Works fine if the URL is remote.

Edit 3:

Seems to work if I wait a couple of seconds after the video has completed the export. Could it be something to have with the file not 100% written to the filesystem?

Edit 4: Video of the problem, in this case it played the 3rd time.

Viktor Gardart
  • 3,782
  • 3
  • 17
  • 24

4 Answers4

3

Here's how I set a AVPlayerLayer with the video working (I think what you're missing is the videoGravity parameter).

let bundle = Bundle.main
let moviePath = bundle.path(forResource: "myVideo", ofType: "mp4")
let moviePlayer = AVPlayer(url: URL(fileURLWithPath: moviePath!))

playerLayer = AVPlayerLayer(player: moviePlayer)
playerLayer.frame = movieView.bounds
playerLayer.videoGravity = AVLayerVideoGravityResizeAspect
movieView.layer.addSublayer(playerLayer)
playerLayer.player?.play()
Jeremy
  • 1,461
  • 12
  • 26
  • Thanks for the reply, but as I said, it's working sometimes. And always if the video is form a remote URL. And since it's the same issues in the AVPlayerViewController I'm assuming it's not because of that. – Viktor Gardart Nov 25 '16 at 16:01
  • Amazingly this seems to have fixed my problem on tvOS. Stranger things happen. :o). Thanks. – Dominic Jun 27 '18 at 09:51
  • Pls suggest here : https://stackoverflow.com/questions/54745359/avplayer-shows-black-screen-when-playing-multiple-videos-in-swift3-ios – Anand Gautam Feb 18 '19 at 10:47
1

It's the frame height or width is equal to zero

nahlamortada
  • 489
  • 6
  • 16
1

i have the same issues as you did. the reason is in iOS 10.xx , if you export video with animationTool . You will meet the trouble like that . try to fix them by remove this code .

something like that

 mainComposition.animationTool =  AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, in: parentlayer)

Hope to help you

0

What caused this issue for me was I was changing assets to play a new video. The problem was I was reinitializing the same AVPlayer and setting setting it to the playerLayer which was previously set

Incorrect

player = AVPlayer()
playerLayer = AVPlayerLayer(player: player)
// ...
player?.replaceCurrentItem(with: playerItem)

Correct

if player == nil {

    player = AVPlayer()
    playerLayer = AVPlayerLayer(player: player)
    // ...
}

player?.replaceCurrentItem(with: playerItem)

Or better yet I should've just called this by itself

player?.replaceCurrentItem(with: playerItem)
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256