我怎样才能检查我的AVPlayer是否缓冲?

我想检测我的AVPlayer是否缓冲当前位置,以便我可以显示一个装载机或其他东西。 但我似乎无法findAVPlayer的文档中的任何东西。

你可以观察你的player.currentItem的值:

 playerItem.addObserver(self, forKeyPath: "playbackBufferEmpty", options: .New, context: nil) playerItem.addObserver(self, forKeyPath: "playbackLikelyToKeepUp", options: .New, context: nil) playerItem.addObserver(self, forKeyPath: "playbackBufferFull", options: .New, context: nil) 

然后

 override public func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) { if object is AVPlayerItem { switch keyPath { case "playbackBufferEmpty": // Show loader case "playbackLikelyToKeepUp": // Hide loader case "playbackBufferFull": // Hide loader } } } 

接受的答案不适合我,我使用下面的代码高效地显示加载程序。

Swift 3

 //properties var observer:Any! var player:AVPlayer! self.observer = self.player.addPeriodicTimeObserver(forInterval: CMTimeMake(1, 600), queue: DispatchQueue.main) { [weak self] time in if self?.player.currentItem?.status == AVPlayerItemStatus.readyToPlay { if let isPlaybackLikelyToKeepUp = self?.player.currentItem?.isPlaybackLikelyToKeepUp { //do what ever you want with isPlaybackLikelyToKeepUp value, for example, show or hide a activity indicator. } } }