0

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 AVPlayer. I am using this code but it's not working :

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
        }
    }
}
pacification
  • 5,838
  • 4
  • 29
  • 51
omid shojaeyan
  • 57
  • 1
  • 10
  • 1
    I was also stuck with this issue for months and today i found this article and my problem solved.I was using Swift 4 and iOS 12 as Base SDK. [Very HelpFul Article For Solving Buffering and Show Loader/Hide Loader](https://medium.freecodecamp.org/how-to-set-up-video-streaming-in-your-app-with-avplayer-7dc21bb82f3) This is working in Swift 4. –  May 10 '19 at 07:38
  • @user9438383 There is nothing in the above link. – shraddha11 Jul 03 '20 at 11:45

0 Answers0