Reputation: 493
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 ?
Upvotes: 7
Views: 4381
Reputation: 497
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")
}
}
}
}
Upvotes: 5
Reputation: 649
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 of1.0
play at the natural rate of the current item.
Apple documentation and this topic.
Hope this helps.
Upvotes: 5