swift. AVPlayer. How to track when song finished playing?

前端 未结 7 597
春和景丽
春和景丽 2021-01-30 13:51

What is the east way to track when song finished playing with AVPlayer in Swift?

Is there any function which is called when avplayer finished playing, or I should combin

7条回答
  •  死守一世寂寞
    2021-01-30 14:46

    for Swift 4.2

    func play(url: URL) {
        let item = AVPlayerItem(url: url)
        NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinishPlaying(sender:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: item)
    
        let player = AVPlayer(playerItem: item)
        player.play() 
    }
    
    @objc func playerDidFinishPlaying(sender: Notification) {
        // Your code here
    }
    

提交回复
热议问题