简体   繁体   English

检查AVPlayer缓冲和播放状态

[英]Check AVPlayer buffering and playing state

I want to know that how i can check that AVPlayer is not playing music when i click on play Button( player.play() ) and it currently (buffering or loading) due to slow internet connection or due to other causes, and also check when it play music after buffering. 我想知道当我单击播放Button( player.play() )时,由于互联网连接速度缓慢或其他原因,我如何检查AVPlayer是否未播放音乐,并且还检查缓冲后播放音乐时。 Because i need to add UIActivityIndicatorView on Play button. 因为我需要在“播放”按钮上添加UIActivityIndicatorView When Play button is clicked it animate till the buffering and when AVPlayer play song after buffering it stop animating and when it AVPlayer again start buffering it again animate. 单击“播放”按钮时,它会进行动画处理直到缓冲;当缓冲后的AVPlayer播放歌曲时,它将停止动画处理;当AVPlayer再次开始进行缓冲时,它将再次进行动画处理。 Can anyone please tell me how i can do this with simple way like this 谁能告诉我如何用这种简单的方法做到这一点

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

and also check again and again using NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: "check", userInfo: nil, repeats: true) 并使用NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: "check", userInfo: nil, repeats: true)一次又一次地NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: "check", userInfo: nil, repeats: true)

Or any other simple solution? 或任何其他简单的解决方案? Thanks. 谢谢。

This seems similar to a question I answered here: https://stackoverflow.com/a/42148482/5100819 这似乎类似于我在这里回答的问题: https : //stackoverflow.com/a/42148482/5100819

The approach we used to update the UI when the AVPlayer finishes buffering and starts actual audible playback would likely do the job. 当AVPlayer完成缓冲并开始实际的音频播放时,我们用来更新UI的方法可能会完成这项工作。 We used a boundary time to execute a block at the start of playback – specifically after 1/3 of a second of playback – to update the UI at roughly the same time as audio is heard: 我们使用边界时间在回放开始时(特别是在回放的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()
})

I have a little more detail on the approach (and a sample Xcode project) on our company blog: http://www.artermobilize.com/blog/2017/02/09/detect-when-ios-avplayer-finishes-buffering-using-swift/ 我在我们公司的博客上详细介绍了该方法(以及Xcode示例项目): http : //www.artermobilize.com/blog/2017/02/09/detect-when-ios-avplayer-finishes-buffering -使用迅捷/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM