using AVKit Player View, how to know when user starts/stops?

programming for macOS, (not iOS), using AVKit Player View which has transport controls built in... I want another part of the app to start a behavior when the user plays a video, and stop it when the user stops the video. How would I do that? I thought I could observe the status, but status turns out only to mean whether the video is loaded and ready to play, or not, very limited in what statuses are available. I looked through the delegate stuff... couldn't find anything.

It also seems like AV Player View is the only way to view a video in macOS, am I right about that? If I didn't use a AVKit Player View, what else would I drag into my view controller storyboard to play videos?

Thanks!
You can use KVO to observe AVPlayer.status. Note that this has been broken in Swift for >3 years, see https://bugs.swift.org/browse/SR-5872 .

So in that case my work-around was to observe .rate instead.
Thank you so much for your reply maven. That worked. I'm relatively new to development and I kind of find it sad and a bit shocking that a function like that can be broken. Although based on the documentation, I never had much hope for .status as it seems to only have three possible states... readyToPlay, failed, and unknown. Doesn't seem like it's possible to know if the video is playing, or paused, or being fast forwarded, etc via the .status keyPath. But rate is just lovely.

I implemented it by adding the observer in my video file open panel modal:

Code Block
avPlayer?.addObserver(self, forKeyPath: "rate", options: NSKeyValueObservingOptions.new, context: nil)


and then a func in my viewController.swift file:

Code Block
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if keyPath == "rate" {
      if avPlayer?.rate ?? 0 > 0 {
        print("video is playing. \(avPlayer?.rate as Any)" )
      } else {
        print("video is NOT playing.")
      }
    }
  }

using AVKit Player View, how to know when user starts/stops?
 
 
Q