How can I check if my AVPlayer is buffering?

后端 未结 10 1072
后悔当初
后悔当初 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 07:01

    The accepted answer didn't work for me, I used the code below to show the loader efficiently.

    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.
            }
        }
    }
    

提交回复
热议问题