【问题标题】:AVPlayer Play, Pause and Buffering issueAVPlayer 播放、暂停和缓冲问题
【发布时间】:2014-09-02 02:40:21
【问题描述】:

我的应用播放流媒体视频,但是当它缓冲时,播放器进入暂停模式,我必须手动将其设置为播放模式,为了处理这种情况,我的 AVPlayer 类中有以下代码,但它不起作用。

在 ViewDidLoad 方法中

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

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

- (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
    }
}

}

是否有其他解决方案可以让玩家继续播放模式?

【问题讨论】:

  • 有人愿意帮助我吗??
  • 你得到答案了吗?
  • 在 iOS 9 中,KVO 不需要这个额外的代码。系统在没有这个的情况下处理它。我不知道如何为 iOS 8 解决这个问题。我正在寻找这个问题的答案

标签: ios objective-c xcode5 avplayer


【解决方案1】:

这可能会有所帮助,

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

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

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 事件向其添加目标。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-12
    • 2018-11-06
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多