迅速。 AVPlayer。歌曲播放完毕后如何追踪? [英] swift. AVPlayer. How to track when song finished playing?

查看:145
本文介绍了迅速。 AVPlayer。歌曲播放完毕后如何追踪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swift中用AVPlayer播放歌曲的东方方式是什么?

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

是否有任何在avplayer播放完成后调用的功能,或者我应该结合使用具有avplayer类引用的计时器?

Is there any function which is called when avplayer finished playing, or I should combine timer with avplayer class references?

推荐答案

这样的工作原理:

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

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

    let player = AVPlayer(playerItem: item)
    player.play()
}

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

完成后不要忘记删除观察者(或 deinit )!

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

这篇关于迅速。 AVPlayer。歌曲播放完毕后如何追踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆