AVPlayer播放,暂停和缓冲问题 [英] AVPlayer Play, Pause and Buffering issue

查看:482
本文介绍了AVPlayer播放,暂停和缓冲问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序播放流式视频,但是当它缓冲时,播放器进入暂停模式,因此我必须将其手动设置为再次播放模式,为了处理这种情况,我在AVPlayer类中添加了以下代码,但这不起作用.

My app plays a streaming video, but when it buffers, the player goes to the pause mode and I have to set it to play mode again manually, I have the following code in my AVPlayer class in order to handle this situation, but it does not work.

在ViewDidLoad方法中

In the ViewDidLoad method

[playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
[playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];

然后使用以下方法处理观察者

and then, handling the observers using the following methods

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                    change:(NSDictionary *)change context:(void *)context {
if (!player)
{
    return;
}

else if (object == playerItem && [keyPath isEqualToString:@"playbackBufferEmpty"])
{
    if (playerItem.playbackBufferEmpty) {
        //Your code here
    }
}

else if (object == playerItem && [keyPath isEqualToString:@"playbackLikelyToKeepUp"])
{
    if (playerItem.playbackLikelyToKeepUp)
    {
        //Your code here
    }
}

}

是否有其他解决方案可以使播放器继续播放模式?

is there a another solution for this problem in order to get the player to continues play mode?

推荐答案

这可能有帮助,

假设这是您的AVPlayer对象
player1 = [AVPlayer playerWithURL:streamURL];

suppose this is your AVPlayer object
player1 = [AVPlayer playerWithURL:streamURL];

当您的视频进入缓冲模式后,您可以暂停播放,并在播放完后再次播放,如下所示: 在观察者方法中,

When your video goes in buffer mode then you can pause it and when it done play it again like: In observer method,

if ([object isKindOfClass:[AVPlayerItem class]])
{
    AVPlayerItem *item = (AVPlayerItem *)object;
    //playerItem status value changed?
    if ([keyPath isEqualToString:@"status"])
    {   //yes->check it...

        NSLog(@"STATUS = %d",item.status);
        switch(item.status)
        {
            case AVPlayerItemStatusFailed:
                NSLog(@"player item status failed");
                break;
            case AVPlayerItemStatusReadyToPlay:

                 [playButton setTitle:@"Pause" forState:UIControlStateNormal];
                [player1 play];

                NSLog(@"player item status is ready to play");
                break;
            case AVPlayerItemStatusUnknown:
                NSLog(@"player item status is unknown");
                break;
        }
    }
    else if ([keyPath isEqualToString:@"playbackBufferEmpty"])
    {
        if (item.playbackBufferEmpty)
        {
            [playButton setTitle:@"Play" forState:UIControlStateNormal];
            [player1 pause];
            NSLog(@"player item playback buffer is empty");
        }
    }
}

,或者您可以保持按钮单击事件. 在屏幕上放置一个按钮以保持播放和暂停,并通过OnClick事件向其添加Target.

or you can maintain on button click event. Place one button on your screen to maintain play and pause and addTarget to it with OnClick event.

这篇关于AVPlayer播放,暂停和缓冲问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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