ios AVPlayer 缓冲、暂停通知和海报帧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10648963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:26:02  来源:igfitidea点击:

AVPlayer buffering, pausing notification, and poster frame

iphoneiosavfoundationavplayer

提问by Omer Waqas Khan

I have some questions related to AVPlayerwhich are:

我有一些相关的问题AVPlayer是:

  1. When we pause the AVPlayerthrough [player pause]does the AVPlayerkeep buffering the video from the network or does it just stop? I couldn't get any info related to this in apple's documentation. Also, is it possible to force the AVPlayer to keep buffering while in pause, so that if we have the paused video is in waiting for the first video to be ended then we wouldn't find any gap in between the videos?

  2. On pausing the AVPlayercan we have any event on [player pause].

  3. Can we show still image on AVPlayerfor some seconds?

  1. 当我们暂停AVPlayer通过时[player pause],是AVPlayer继续缓冲来自网络的视频还是只是停止?我无法在苹果的文档中获得任何与此相关的信息。此外,是否可以强制 AVPlayer 在暂停时保持缓冲,以便如果我们暂停的视频正在等待第一个视频结束,那么我们不会在视频之间发现任何间隙?

  2. 在暂停时,AVPlayer我们在 上有任何事件[player pause]

  3. 我们可以显示AVPlayer几秒钟的静止图像吗?

Thanks

谢谢

回答by djromero

1) AVPlayerwill buffer the video in several cases, none cleary documented. I'd say you can expect buffering when you init the video, and when you replace the current item. You can observe currentItem.loadedTimeRangesto know what's going on. That property will tell you which video time ranges has been loaded.

1)AVPlayer将在几种情况下缓冲视频,没有明确记录。我想说的是,当您初始化视频和替换当前项目时,您可以期待缓冲。你可以观察currentItem.loadedTimeRanges到知道发生了什么事情。该属性将告诉您已加载哪些视频时间范围。

Also, there is a few other currentItemproperties that may help you: playbackLikelyToKeepUp, playbackBufferFulland playbackBufferEmpty.

此外,还有一些其他currentItem属性可以帮助您:playbackLikelyToKeepUp,playbackBufferFullplaybackBufferEmpty

Achieving a perfect gapless playback is not easy.

实现完美的无缝播放并不容易。

/* player is an instance of AVPlayer */
[player addObserver:self 
         forKeyPath:@"currentItem.loadedTimeRanges" 
            options:NSKeyValueObservingOptionNew 
            context:kTimeRangesKVO];    

In observeValueForKeyPath:ofObject:change:context::

observeValueForKeyPath:ofObject:change:context:

if (kTimeRangesKVO == context) {
   NSArray *timeRanges = (NSArray *)[change objectForKey:NSKeyValueChangeNewKey];
   if (timeRanges && [timeRanges count]) {
       CMTimeRange timerange = [[timeRanges objectAtIndex:0] CMTimeRangeValue];
       NSLog(@" . . . %.5f -> %.5f", CMTimeGetSeconds(timerange.start), CMTimeGetSeconds(CMTimeAdd(timerange.start, timerange.duration)));
   }
}

2) Just keep an eye on player.rate.

2) 只关注player.rate.

[player addObserver:self 
         forKeyPath:@"rate" 
            options:NSKeyValueObservingOptionNew 
            context:kRateDidChangeKVO];

Then in your observeValueForKeyPath:ofObject:change:context::

然后在您的observeValueForKeyPath:ofObject:change:context:

    if (kRateDidChangeKVO == context) {
        NSLog(@"Player playback rate changed: %.5f", player.rate);
        if (player.rate == 0.0) {
            NSLog(@" . . . PAUSED (or just started)");
        }
    }

3) You can build a movie of a given length using a still imagebut it's easier to use a regular UIImageViewon top of the player. Hide/show it when needed.

3)您可以使用静止图像构建给定长度的电影,UIImageView在播放器顶部使用常规更容易。需要时隐藏/显示它。

Sample project: feel free to play with the code I wrote to support my answer.

示例项目:随意使用我编写的代码来支持我的答案