Search code examples
swiftavplayer

two AVPlayers, how to know which one finished playing


If I have one AVPlayer, I could monitor the AVPlayerItemDidPlayToEndTime. But if I have two AVPlayers, I wouldn't know which one finished. Is there a way to know which finishes?


Solution

  • You can try

    @objc func playerDidFinishPlaying( _ sender: Notification) {
       if let _ = sender.object as? Player1 {
       }
    }
    

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