当 AVPlayer 准备好播放时,addObserver 到 AVPlayer 永远不会被调用?

addObserver to AVPlayer never being called when AVPlayer Ready to play?

我已经将观察者添加到 AVPlayer,但是当 avplayer 状态准备好播放时它没有调用该函数。该函数在视频开始播放时调用两次,但 AVPlayer return 'nan' 的持续时间参数和我需要在 AVPlayer 准备好播放时获取持续时间。

    playerController.player = player
    self.addChildViewController(playerController)
    self.view.addSubview(playerController.view)
    playerController.view.frame = self.view.frame
    playerController.showsPlaybackControls = false
    player.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions(), context: nil)
    player.play()

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    if player.currentItem?.status == AVPlayerItemStatus.ReadyToPlay {
      print("a")  
    }
}

像这样替换 addObserver 行:

player.currentItem!.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions(), context: nil)