Created
February 1, 2017 20:58
-
-
Save swillits/e0d9ddd8dd597501ab7360ebfbccce07 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using https://gist.github.com/swillits/93206997e7d1bff4d08e24e23173820e | |
extension AVPlayer { | |
private struct AssociatedKeys { | |
static var statusChangeHandler = "ag_statusChangeHandler" | |
static var statusReceptionist = "ag_statusReceptionist" | |
} | |
var statusChangeHandler: ((AVPlayerStatus) -> ())? { | |
get { | |
return objc_getAssociatedObject(self, &AssociatedKeys.statusChangeHandler) as? ((AVPlayerStatus) -> ()) | |
} | |
set { | |
if let closure: Any = newValue { | |
objc_setAssociatedObject(self, &AssociatedKeys.statusChangeHandler, closure, .OBJC_ASSOCIATION_COPY) | |
statusReceptionist = Receptionist(forKeyPath: "status", of: self, handler: { [weak self] info in | |
if let player = self { | |
player.statusChangeHandler?(player.status) | |
} | |
}) | |
} else { | |
objc_setAssociatedObject(self, &AssociatedKeys.statusChangeHandler, nil, .OBJC_ASSOCIATION_COPY) | |
statusReceptionist = nil | |
} | |
} | |
} | |
private var statusReceptionist: Receptionist? { | |
get { | |
return objc_getAssociatedObject(self, &AssociatedKeys.statusReceptionist) as? Receptionist | |
} | |
set { | |
objc_setAssociatedObject(self, &AssociatedKeys.statusReceptionist, newValue, .OBJC_ASSOCIATION_RETAIN) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment