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

时间:2015-12-29 11:34:22

标签: ios swift avplayer observers ios9.1

我已将观察者添加到AVPlayer但是当avplayer状态准备好播放时它没有调用该功能。该视频开始播放时调用两次,但AVPlayer的持续时间参数都返回' 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")  
    }
}

1 个答案:

答案 0 :(得分:6)

替换addObserver这样的行:

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