AVPlayer停止播放并再次不恢复 [英] AVPlayer stops playing and dont resume again

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

问题描述

在我的应用程序必须播放存储在Web服务器上的音频文件。即时通讯使用 AVPlayer 它。我把所有的播放/暂停控制和完全正常工作的所有代表和观察员那里。在打小的音频文件一切都很正常。

In my application I have to play audio files stored on a web server. Im 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 暂停播放(最有可能缓冲的话)。问题是它没有对自己再次恢复。它保持暂停状态,如果我手动preSS播放按钮再次自如再次播放。

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 don'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 不自动恢复,我怎么能设法再次恢复音频,而无需用户$ P $再次pssing播放按钮?谢谢你。

I want to know why AVPlayer dont 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 has follow this steps:

1)检测:检测当玩家已经停止我用的是韩国国际志愿者组织与价值的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];
        }
      }
    }

这种情况:<!code> 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 to the end of the video and stop in the middle

2)等待视频加载 - 如果继续打,直接你不会有足够的缓冲区继续播放,无需中断。要知道什么时候开始,你必须观察值 playbackLikelytoKeepUp 从playerItem(这里我用一个库块来观察,但我认为它使点):

2)Wait 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];
            }
                    }
    }];
}

这就是它!问题就解决了​​。

And that's it! problem solved

编辑:顺便说一下使用的库libextobjc

By the way the library used is libextobjc

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

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