我有AVPlayer
在线音频流& AVQueuePlayer
用于离线队列播放多首歌曲。
mycode的
self.myQueuePlayer?.addObserver(self, forKeyPath: "timedMetadata", options: NSKeyValueObservingOptions.new, context: nil)
myPlayer.currentItem?.addObserver(self, forKeyPath: "timedMetadata", options: [.new,.old,.initial], context: nil)
我如何观察KVO" timedMetadata" &安培; get是AVPlayer
还是AVQueuePlayer
?
答案 0 :(得分:0)
您可以使用context参数来区分不同的KVO对象:
// class
private var playerContext = 0
private var queuePlayerContext = 0
func setup() {
self.myQueuePlayer?.addObserver(self, forKeyPath: "timedMetadata", options: .new, context: &queuePlayerContext)
myPlayer.currentItem?.addObserver(self, forKeyPath: "timedMetadata", options: .new, context: &playerContext)
}
override open func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard let context = context else {
return
}
if keyPath == "timedMetadata" {
switch context {
case &queuePlayerContext:
print("queuePlayer")
case &playerContext:
print("playerContext")
default:
break
}
}
}