如何检测媒体播放器何时完成播放?

时间:2018-07-14 20:14:17

标签: ios swift xcode

这可能是我一个简单的问题,但是如何检测 MPMediaPlayer 在Swift 4中播放的时间?

1 个答案:

答案 0 :(得分:1)

您可以使用NSNotificationCenter进行跟踪。

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
 }