当歌曲在 Swift 中使用 AVPlayer 播放完毕时,最简单的跟踪方式是什么?
AVPlayer 播放结束时是否有调用函数,或者我应该将计时器与 AVPlayer 类引用结合起来?
最佳答案
类似这样的东西:
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
中)移除观察者!
关于ios - 如何在 Swift 中使用 AVPlayer 跟踪歌曲何时播放完毕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27805657/