我正在使用AVPlayer从网址播放视频. AVPlayerItemDidPlayToEndTimeNotification未触发.我已经把断点检查了.以下是我的代码片段: –
@H_502_2@@IBAction func playButtonClicked(sender: UIButton) {
let url:NSURL = NSURL(string: self.currentSelectedContent.link)!
moviePlayer = AVPlayer(URL: url)
playerViewController = AVPlayerViewController()
playerViewController.player = moviePlayer
self.presentViewController(playerViewController,animated: true) {
NSNotificationCenter.defaultCenter().addObserver(self,selector: "moviePlayBackFinished",name: AVPlayerItemDidPlayToEndTimeNotification,object: self.moviePlayer)
self.playerViewController.player?.play()
}
}
func moviePlayBackFinished() {
self.playerViewController.dismissViewControllerAnimated(true,completion: nil)
}
解决方法
根据文档,观察对象必须是AVPlayerItem实例,而不是AVPlayer本身.尝试将self.moviePlayer更改为self.moviePlayer.currentItem.