How can I check if my AVPlayer is buffering?

后端 未结 10 1063
后悔当初
后悔当初 2020-12-01 06:02

I want to detect if my AVPlayer is buffering for the current location, so that I can show a loader or something. But I can\'t seem to find anything in the documentation for

10条回答
  •  無奈伤痛
    2020-12-01 06:59

    Updated for Swift 4.2

        var player : AVPlayer? = nil
    
        let videoUrl = URL(string: "https://wolverine.raywenderlich.com/content/ios/tutorials/video_streaming/foxVillage.mp4")
        self.player = AVPlayer(url: videoUrl!)
        self.player?.addPeriodicTimeObserver(forInterval: CMTimeMake(value: 1, timescale: 600), queue: DispatchQueue.main, using: { time in
    
            if self.player?.currentItem?.status == AVPlayerItem.Status.readyToPlay {
    
                if let isPlaybackLikelyToKeepUp = self.player?.currentItem?.isPlaybackLikelyToKeepUp {
                    //do what ever you want with isPlaybackLikelyToKeepUp value, for example, show or hide a activity indicator.
    
                    //MBProgressHUD.hide(for: self.view, animated: true)
                }
            }
        })
    

提交回复
热议问题