【问题标题】:Check AVPlayer buffering and playing state检查 AVPlayer 缓冲和播放状态
【发布时间】:2017-02-10 12:03:26
【问题描述】:

我想知道,当我点击播放按钮 (player.play()) 时,我如何检查 AVPlayer 没有播放音乐,并且由于互联网连接速度慢或其他原因,它当前(缓冲或加载),并检查缓冲后何时播放音乐。 因为我需要在播放按钮上添加UIActivityIndicatorView。当单击播放按钮时,它会动画直到缓冲,当 AVPlayer 在缓冲后播放歌曲时停止动画,当它AVPlayer 再次开始缓冲它时再次动画。 谁能告诉我如何用这样的简单方法做到这一点

if (player is buffering){
    activityIndicator.tartAnimating()
}else if player is playing music{
    activityIndicator.stopAnimating()
}

并且还使用NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: "check", userInfo: nil, repeats: true)反复检查

或任何其他简单的解决方案? 谢谢。

【问题讨论】:

标签: ios objective-c swift avplayer


【解决方案1】:

这似乎类似于我在这里回答的一个问题:https://stackoverflow.com/a/42148482/5100819

当 AVPlayer 完成缓冲并开始实际的声音播放时,我们用来更新 UI 的方法可能会完成这项工作。我们使用边界时间在播放开始时(特别是在播放 1/3 秒后)执行块,以便在听到音频的大致同一时间更新 UI:

let times = [NSValue(time:CMTimeMake(1,3))]
_ = self.player.addBoundaryTimeObserver(forTimes: times, queue: DispatchQueue.main, using: {
    [weak self] time in
    // Code to update the UI goes here, e.g.
    activityIndicator.stopAnimating()
})

我在我们公司的博客上提供了有关该方法(和示例 Xcode 项目)的更多详细信息:http://www.artermobilize.com/blog/2017/02/09/detect-when-ios-avplayer-finishes-buffering-using-swift/

【讨论】:

    猜你喜欢
    • 2011-08-05
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    相关资源
    最近更新 更多