Here is my code to start the video:
let videoURL = NSURL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let playerAV = AVPlayer(url: videoURL! as URL)
let playerLayerAV = AVPlayerLayer(player: playerAV)
playerLayerAV.frame = ...
self.view.layer.addSublayer(playerLayerAV)
playerAV.play()
I can see the video preview, I can also see it's being loaded, I can even move the slider and see that preview is changing to the time I'm choosing. But if I hit play - nothing happens. I tried to press "play" button manually, I tried to call .play
programmatically, the state of the "Play" button changes to "||" but then switches back to ">" right away.
I'm also tracking debug info related to the player state and I notice the following. Right when I created a player the status is below:
Status: ReadyToPlay
TimeControl: WaitingToPlayAtSpecifiedRate
ReasonToWait: AVPlayerWaitingToMinimizeStallsReason
Time: {0/1 = 0.000}
Rate: 1
Loaded Time Ranges: [Location=0,Length=0]
And it almost instantly changes its TimeControl: Paused and Rate: 0:
Status: ReadyToPlay
TimeControl: Paused
ReasonToWait:
Time: {0/1 = 0.000}
Rate: 0
Loaded Time Ranges: [Location=0,Length=0]
In both cases, it shows that playerItem.LoadedTimeRanges
has one element but length and locations are 0 when the UI shows that the video has been completely loaded and ready to play.
Turned out that there is no issue with the video, views or controllers setup. The video is loaded and ready to play and paused right away because of a mode of the share audioSession
. It was previously set to .record
to process another app use-case.
The solution is to restore an original audioSession
mode which includes playback and the video will play without any issues.