AVPlayer 停止播放并且不再恢复 [英] AVPlayer stops playing and doesn't resume again

查看:26
本文介绍了AVPlayer 停止播放并且不再恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我必须播放存储在 Web 服务器上的音频文件.我正在使用 AVPlayer .我有所有的播放/暂停控件以及所有的代表和观察者,它们工作得非常好.在播放小音频文件时一切正常.

In my application I have to play audio files stored on a web server. I'm using AVPlayer for it. I have all the play/pause controls and all delegates and observers there which work perfectly fine. On playing small audio files everything works great.

当播放长音频文件时,它也开始正常播放,但几秒钟后 AVPlayer 暂停播放(很可能是为了缓冲它).问题是它不会再次自行恢复.它保持暂停状态,如果我再次手动按下播放按钮,它会再次流畅播放.

When a long audio file is played it also starts playing fine but after some seconds the AVPlayer pauses the playing (most probably to buffer it). The issue is it doesn't resume on its own again. It keeps in a pause state and if I manually press the play button again it plays smoothly again.

我想知道为什么 AVPlayer 没有自动恢复,我怎样才能在没有用户再次按下播放按钮的情况下再次恢复音频?谢谢.

I want to know why AVPlayer doesn't resume automatically and how can I manage to resume the audio again without user pressing the play button again? Thanks.

推荐答案

是的,它会停止,因为缓冲区为空,因此它必须等待加载更多视频.之后,您必须再次手动要求启动.为了解决这个问题,我按照以下步骤操作:

Yes, it stops because the buffer is empty so it has to wait to load more video. After that you have to manually ask for start again. To solve the problem I followed these steps:

1) 检测:为了检测玩家何时停止,我使用 KVO 和值的 rate 属性:

1) Detection: To detect when the player has stopped I use the KVO with the rate property of the value:

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"rate"] )
    {

        if (self.player.rate == 0 && CMTimeGetSeconds(self.playerItem.duration) != CMTimeGetSeconds(self.playerItem.currentTime) && self.videoPlaying)
        {
            [self continuePlaying];
        }
      }
    }

这个条件:CMTimeGetSeconds(self.playerItem.duration) != CMTimeGetSeconds(self.playerItem.currentTime) 是检测到达视频结尾或中途停止的区别

This condition: CMTimeGetSeconds(self.playerItem.duration) != CMTimeGetSeconds(self.playerItem.currentTime) is to detect the difference between arriving at the end of the video or stopping in the middle

2) 等待视频加载 - 如果您继续直接播放,您将没有足够的缓冲区来不间断地继续播放.要知道何时开始,您必须观察 playerItem 中的 playbackLikelytoKeepUp 值(这里我使用一个库来观察块,但我认为这很重要):

2) Wait for the video to load - If you continue playing directly you will not have enough buffer to continue playing without interruption. To know when to start you have to observe the value playbackLikelytoKeepUp from the playerItem (here I use a library to observe with blocks but I think it makes the point):

-(void)continuePlaying
 {

if (!self.playerItem.playbackLikelyToKeepUp)
{
    self.loadingView.hidden = NO;
    __weak typeof(self) wSelf = self;
    self.playbackLikelyToKeepUpKVOToken = [self.playerItem addObserverForKeyPath:@keypath(_playerItem.playbackLikelyToKeepUp) block:^(id obj, NSDictionary *change) {
        __strong typeof(self) sSelf = wSelf;
        if(sSelf)
        {
            if (sSelf.playerItem.playbackLikelyToKeepUp)
            {
                [sSelf.playerItem removeObserverForKeyPath:@keypath(_playerItem.playbackLikelyToKeepUp) token:self.playbackLikelyToKeepUpKVOToken];
                sSelf.playbackLikelyToKeepUpKVOToken = nil;
                [sSelf continuePlaying];
            }
                    }
    }];
}

就是这样!问题解决

顺便说一下,使用的库是 libextobjc

By the way the library used is libextobjc

这篇关于AVPlayer 停止播放并且不再恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆