gpt4 book ai didi

ios - 检测 AVPlayer 和 AVQueuePlayer KVO 之间的差异

转载 作者:行者123 更新时间:2023-11-28 08:26:42 26 4
gpt4 key购买 nike

我有一个用于在线音频流的 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)

我如何观察“timedMetadata”的 KVO 并获取它是 AVPlayer 还是 AVQueuePlayer

最佳答案

你可以使用上下文参数来区分不同的 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
}
}
}

关于ios - 检测 AVPlayer 和 AVQueuePlayer KVO 之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39791226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com