1

I'm pretty new to observers and Swift/ObjC, and I'm trying to monitor AVPlayer status using one as said in Apple's documentation, but everything I get is a "message was received but not handled" error message when I try to add the observer.

gs_mediaObjAdv=AVPlayer(URL: NSURL(string: mediaURL));
gs_mediaObjAdv.addObserver(self, forKeyPath: "status", options:NSKeyValueObservingOptions.New, context:nil);

If I unregister the observer, the error disappear but if I add a println("test") inside the observeValueForKeyPath function, nothing happens.

Any idea in how can I monitor the status in a simple way?

Greensouth
  • 41
  • 1
  • 9
  • what is your `observeValueForKeyPath` code – Bot Mar 20 '15 at 21:31
  • have you made a super call such as `[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];` ? I think the issue might exist if your super isn't KVO compliant. Try commenting the code. – GoGreen May 13 '15 at 10:51
  • did you got the solution? I have the same issue. – Ruchi Oct 21 '19 at 12:07

2 Answers2

8

Please check for the Observer method which you have an override. It has to be an instance method instead of a class method.

Martin
  • 846
  • 1
  • 9
  • 23
0

here is the answer for whoever else needs it Swift 5.1

//playerViewController is defined as a class member 
//let playerViewController = AVPlayerViewController()
self.playerViewController.player!.addObserver(self, forKeyPath: "rate", options: .new, context: nil)

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if keyPath == "rate" {
        if let rate = change?[NSKeyValueChangeKey.newKey] as? Float {
            if rate == 0.0 {
                print("playback stopped")
                //view.addSubview(drawView)
            }
            if rate == 1.0 {
                print("normal playback")
            }
            if rate == -1.0 {
                print("reverse playback")
            }
        }
    }
    super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
Gal Blank
  • 2,070
  • 2
  • 18
  • 18