Mega4alik Mega4alik - 1 year ago 133
Swift Question

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

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 combine timer with avplayer class references?

Answer Source

Something like this works:

func play(url: NSURL) {
    let item = AVPlayerItem(URL: url)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: item)

    let player = AVPlayer(playerItem: item)

func playerDidFinishPlaying(note: NSNotification) {
    // Your code here

Don't forget to remove the observer when you're done (or in deinit)!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download