我已将观察者添加到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")
}
}
答案 0 :(得分:6)
替换addObserver
这样的行:
player.currentItem!.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions(), context: nil)