How can I obtain gapless playback with AVPlayer?

147 Views Asked by At

I am trying to play files consecutively with no gap in between. The first thing I tried was using AVQueuePlayer:

let player = AVQueuePlayer()

let playerItem1 = ...
let playerItem2 = ...

player.insert(playerItem1, after: nil)
player.insert(playerItem2, after: nil)

player.play()

This works great and exactly as expected. However, there are some shortcoming of AVQueuePlayer and I've decided to instead use AVPlayer with a custom queue implementation. The problem I am facing is that although AVQueuePlayer is a subclass of AVPlayer, I cannot get AVPlayer to be gapless. Here's how the code is set up:

let playerItem1 = ...
let playerItem2 = ...

let player = AVPlayer(playerItem: playerItem1)

NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)

player.play()

@objc func playerDidFinishPlaying() {
    player.replaceCurrentItem(with: nextPlayerItem)
    player.play()
}

As you can see, I detect the end of playback of the first item with the AVPlayerItemDidPlayToEndTime notification, and then begin playback of the second item. But with this implementation there is a gap between the two items.

How can I change this so that it behaves more like AVQueuePlayer, with no gap?

(Please note that I am not interested in using AVAudioEngine at this time.)

0

There are 0 best solutions below