11

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)
}
Keshav Raj
  • 199
  • 3
  • 11

3 Answers3

23

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

davidgoli
  • 2,436
  • 1
  • 16
  • 13
5

the property actionAtItemEnd of AVPlayer is KVO compliant:

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

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

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
kholl
  • 609
  • 1
  • 6
  • 20
4

This works for me:

NotificationCenter.default.addObserver(self, 
selector:#selector(didEndPlayback), 
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object:nil)
atulkhatri
  • 10,896
  • 3
  • 53
  • 89
  • what is `NativeVideoPlayer`? – Vyachaslav Gerchicov Jun 11 '19 at 14:37
  • Sorry, that was my code which I was using for one of my projects. Edited my answer. You have to write any selector you want to call when the Notification is fired. Please note that the object `nil` is here because this way it would be fired for every item. You can of course specify a single player item here if you want it for only a single item. – atulkhatri Jun 11 '19 at 16:04
  • in my case it doesn't work. I just wanted to play the item for one time but it is played in infinite loop – Vyachaslav Gerchicov Jun 12 '19 at 07:55
  • Looks like some problem with the code in your `didEndPlayback` selector. You can share the code for more help. – atulkhatri Jun 12 '19 at 07:58
  • in my case there are multiple players and each of them plays a single file. I don't understand why it happens. But in my case I replaced the existing player handler implementation with my own one. The main difference between these implementations - I don't use `AVPlayerItem` explicitly - the player is initialised with filename directly. – Vyachaslav Gerchicov Jun 12 '19 at 13:52