ios - 如何在 Swift 中使用 AVPlayer 跟踪歌曲何时播放完毕

标签 ios swift avplayer

当歌曲在 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/

相关文章:

ios - swift 3 : Transfer Utility enumerateToAssignBlocks method signature

ios - 应用扩展 "Terminated due to memory issue"

ios - 当我使用数组时 Xcode 8 永远索引

iOS: AVQueuePlayer/AVPlayerItem 'An AVPlayerItem can occupy only one position in a player' s queue at a time.'

ios - AVPlayer 隐藏式字幕打开/关闭

ios - Xcode 6.2 beta 和 iOS 8 模拟器 : location manager authorization dialogue fails to appear: bug or failure to understand?

ios - 在 IOS 中登录 Withings OAuth API 时出现错误

ios - 重新加载 UITableView 时出现 NSInvalidArgumentException

swift - UIWebView 未显示(Swift)

objective-c - Assets 加载后立即定位到 AVPlayer 中的某个位置