AVPlayer show cached data
How to show cached data in UISli开发者_运维知识库der from AVPlayer? I play audio with AVPlayer from network, and i want to show downloaded data.
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.
AVPlayer streaming progress
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)
}
}
精彩评论