I need to observe the status of an AVPlayer
, so I add an observer:
let player = AVPlayer(URL: NSURL(fileURLWithPath: tempFilePath))
player.addObserver(self, forKeyPath: "status", options: nil, context: nil)
And observe the change with the relevant function:
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if object is AVPlayer {
var player = object as! AVPlayer
player.removeObserver(self, forKeyPath: "status")
}
}
This is working fine, however, I need to some more information in the observeValueForKeyPath
function, namely a dictionary:
var context = [
"layer": CALayer(),
"filePath": String()
]
Consequently, I figured I could pass that dictionary under the context parameter, since:
The context pointer can be used as a unique identifier to determine the change that is being observed, or to provide some other data to the observer (Source)
I'm not entirely sure how to pass the context
variable, which has a [String, AnyObject]
type as an UnsafeMutablePointer<Void>
type.
Reading this, I figured I could do something like:
UnsafeMutablePointer(Unmanaged.passUnretained(context).toOpaque())
...
Unmanaged<[String, AnyObject]>.fromOpaque(COpaquePointer(context)).takeUnretainedValue()
But not surprisingly, that didn't work. I also figured I could pass the variable by reference, &context
, but I was unable to decode the address of the original variable. Is this where NSCoder
is used?