AVPlayerItemDidPlayToEndTimeNotification未触发

Kes*_*Raj 7 ios avplayer swift avplayerviewcontroller

我正在使用AVPlayer从网址播放视频. AVPlayerItemDidPlayToEndTimeNotification未触发.我已经把断点检查了.以下是我的代码片段: -

    @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)
}
Run Code Online (Sandbox Code Playgroud)

dav*_*oli 17

根据文档,观察对象必须是AVPlayerItem实例,而不是AVPlayer自身.尝试self.moviePlayer改为self.moviePlayer.currentItem.


kho*_*oll 5

AVPlayer的属性actionAtItemEnd符合KVO:

moviePlayer.addObserver(self, forKeyPath: "actionAtItemEnd", options: [], context: nil)


override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    if keyPath == "actionAtItemEnd"{
        // 
        print("FINISH")
    }
}
Run Code Online (Sandbox Code Playgroud)

https://developer.apple.com/documentation/avfoundation/avplayer/1387376-actionatitemend

https://developer.apple.com/documentation/avfoundation/avplayeractionatitemend