2011-09-01 76 views
0

如何顯示來自AVPlayer的UISlider中的緩存數據?我使用AVPlayer從網絡播放音頻,並且我想顯示下載的數據。AVPlayer顯示緩存數據

+1

我有什麼工作,看這裏: http://stackoverflow.com/questions/7691854/avplayer-streaming-進度/ 7730708#7730708 –

+0

非常感謝 –

回答

1

正如評論 - 有一種方法可以從avplayer獲取時間範圍,顯示從遠程URL下載的數據量。

AVPlayer streaming progress

0

,應該注意的財產loadedTimeRanges您player`s CURRENTITEM。 這樣的:

guard let currentItem = self.player?.currentItem else {return} 
currentItem.addObserver(self, forKeyPath: "loadedTimeRanges", options: NSKeyValueObservingOptions.New, context: nil) 

然後實現以下功能:

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