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

前端 未结 7 587
春和景丽
春和景丽 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:51

    You need to create an object that implements the AVAudioPlayerDelegate protocol, and use that as the delegate of the AVAudioPlayer object. Then link them together, for example:

    audioPlayer = try! AVAudioPlayer(contentsOf: audioFileUrl)
    audioPlayer.delegate = self
    

    The delegate can implement methods that responds to certain events. This one fires when the audio finishes playing:

    func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
        // ...
    }
    

提交回复
热议问题