Search code examples
swiftkey-value-observingavplayerlayer

How would someone observe AVPlayerLayer's videoRect changing?


The Apple Documentation states that videoRect responds to KVO, although I can't figure out how to implement it: https://developer.apple.com/documentation/avfoundation/avplayerlayer/1385745-videorect

I have a playerLayer, but how do I add an observer for the videoRect?

playerLayer = AVPlayerLayer(player: player!)

self.playerLayer?.videoRect.addObserver(self, forKeyPath:"test", options: [.old, .new], context: nil)

My problem is that I'm trying to align an image that depends on the videoRect, but the videoRect is set to all 0's in viewDidLoad. A short moment later, it changes, so I need to observe when it changes.


Solution

  • You observe the playerLayer, not the videoRect. The "videoRect" is the key path.