6

I'm using the AVPlayer class to read streams. I have to monitor playback.

Here is my question : Is it possible to detect when the player is stopped by the user ?

I looked at MPMoviePlayerController. If the user stopped the video, this controller sends a notification : MPMovieFinishReasonUserExited. Is there an equivalent ?

tcacciatore
  • 483
  • 6
  • 21
  • [refer](http://stackoverflow.com/questions/6837002/no-avplayer-delegate-how-to-track-when-song-finished-playing-objective-c-iphon) . Hope this helps. – Bista Mar 18 '15 at 09:52
  • I tried but the notification "AVPlayerItemDidPlayToEndTimeNotification" is never by the player. – tcacciatore Mar 18 '15 at 10:12
  • Possible duplicate of [How to detect when AVPlayer video ends playing?](http://stackoverflow.com/questions/29386531/how-to-detect-when-avplayer-video-ends-playing) – Rachit Rawat Jan 13 '17 at 05:42

2 Answers2

5

You can monitor rate property by adding observer on the player for key rate.

A value of 0.0 means pauses the video, while a value of 1.0 play at the natural rate of the current item.

Apple documentation and this topic.

Hope this helps.

Community
  • 1
  • 1
Thlbaut
  • 649
  • 7
  • 25
4

here's the swift 3 code for @Thlbaut's answer

self.avPlayer?.addObserver(self, forKeyPath: "rate", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)

then

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if keyPath == "rate" {
        if let playRate = self.avPlayer?.rate {
            if playRate == 0.0 {
                print("playback paused")
            } else {
                print("playback started")
            }
        }
    }
}
Rezwan
  • 477
  • 5
  • 19