AVPlayer 显示缓存数据

发布于 2024-12-02 16:22:34 字数 71 浏览 0 评论 0原文

如何从 AVPlayer 中显示 UISlider 中的缓存数据?我使用 AVPlayer 从网络播放音频,我想显示下载的数据。

How to show cached data in UISlider from AVPlayer? I play audio with AVPlayer from network, and i want to show downloaded data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

又爬满兰若 2024-12-09 16:22:34

正如评论中所述 - 有一种方法可以从 avplayer 获取时间范围,显示从远程 URL 下载了多少数据。

AVPlayer 流式传输进度

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

注定孤独终老 2024-12-09 16:22:34

您应该观察播放器的 currentItem 的 returnedTimeRanges 属性。
像这样:

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

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)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文