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

查看:333
本文介绍了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天全站免登陆