Ios – AVPlayerItemDidPlayToEndTimeNotification not firing

avplayeravplayerviewcontrolleriosswift

I am playing video from the url using AVPlayer. AVPlayerItemDidPlayToEndTimeNotification is not firing. I have put the breakpoints to check. Below is my code snippet:-

    @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)
}

Best Answer

According to the docs, the observed object must be the AVPlayerItem instance, not the AVPlayer itself. Try changing self.moviePlayer to self.moviePlayer.currentItem.

Related Topic