Search code examples
swiftavqueueplayer

How can I detect that AVQueuePlayer finished playing?


I am playing back a queue of multiple audio files using AVQueuePlayer. I have not been able to detect that the last audio file finished playback.

For AVAudioPlayer there is AVAudioPlayerDelegate with audioPlayerDidFinishPlaying, however that seems not to apply to AVQueuePlayer.

How do I detect that AVQueuePlayer finished playback?


Solution

  • Figured it out. Need to add before inserting an item:

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

    which then calls

    @objc func playerDidFinishPlaying(sender: Notification) {
        print("Finished playing")
    }