How to show cached data in UISlider from AVPlayer? I play audio with AVPlayer from network, and i want to show downloaded data.
Asked
Active
Viewed 1,824 times
0
-
1I have something working, look here: http://stackoverflow.com/questions/7691854/avplayer-streaming-progress/7730708#7730708 – Andrew Kuklewicz Oct 11 '11 at 18:43
2 Answers
1
As in the comment - there is a way to get time ranges from an avplayer that shows how much data is downloaded from a remote URL.

Community
- 1
- 1

Andrew Kuklewicz
- 10,621
- 1
- 34
- 42
0
You should observe the property loadedTimeRanges for your player`s currentItem. like this:
guard let currentItem = self.player?.currentItem else {return}
currentItem.addObserver(self, forKeyPath: "loadedTimeRanges", options: NSKeyValueObservingOptions.New, context: nil)
then realize the function:
observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
guard object is AVPlayerItem else {return}
let item = object as! AVPlayerItem
if keyPath == "loadedTimeRanges" {
let array = item.loadedTimeRanges
guard let timeRange = array.first?.CMTimeRangeValue else {return}
let totalBuffer = CMTimeGetSeconds(timeRange.start) + CMTimeGetSeconds(timeRange.duration)
tmpTime = CGFloat(tmpTime)
print("totalBuffer - \(totalBuffer)")
let tmpProgress = tmpTime / playDuration
progressCallBack?(tmpProgress: Float(tmpProgress), playProgress: nil)
}
}

Azen Xu
- 1
- 1