检测AVPlayer和amp;之间的区别AVQueuePlayer KVO

时间:2016-09-30 12:10:23

标签: ios iphone swift avplayer key-value-observing

我有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

1 个答案:

答案 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
        }
    }
}