如何检测AVPlayer竟然开始快速播放

你好,我已经把我的UISlider最小值设置为0.00。 然后我以这种方式设置它的最大值。

 self.viewPlayer.layer.addSublayer(playerLayer) let duration : CMTime = avPlayer.avPlayer.currentItem!.asset.duration let seconds : Float64 = CMTimeGetSeconds(duration) sliderBar.maximumValue=Float(seconds) sliderBar!.isContinuous = false sliderBar!.tintColor = UIColor.green 

但是我得到这个例外

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempting to set a slider's minimumValue (0.000000) to be larger than the maximumValue (nan)' enter code here 

我知道prepareForPlay()后真正播放它需要一些时间才能真正播放video。 那么我怎样才能检测到玩家真正开始播放video? 请帮帮我。 谢谢

您可以像这样在您的AVPlayer的对象上添加观察者

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

你可以用你的AVPlayer检查状态变化

  func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutableRawPointer) { if keyPath == "status" { print(player.status) } } 

这是我做了什么实际上知道什么时候开始video(而不是只准备开始)。

斯威夫特4

 player = AVPlayer(url: URL(fileURLWithPath: path)) player.addObserver(self, forKeyPath: "rate", options: NSKeyValueObservingOptions.new, context: nil) override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if keyPath == "rate" { if player.rate > 0 { print("video started") } } } 

从Apple的文档 : You can observe an AVPlayerLayer object's readyForDisplay property to be notified when the layer has user-visible content. In particular, you might insert the player layer into the layer tree only when there is something for the user to look at and then perform a transition from You can observe an AVPlayerLayer object's readyForDisplay property to be notified when the layer has user-visible content. In particular, you might insert the player layer into the layer tree only when there is something for the user to look at and then perform a transition from