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
AVPlayer buffering, pausing notification, and poster frame
提问by Omer Waqas Khan
I have some questions related to AVPlayer
which are:
我有一些相关的问题AVPlayer
是:
When we pause the
AVPlayer
through[player pause]
does theAVPlayer
keep 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?On pausing the
AVPlayer
can we have any event on[player pause]
.Can we show still image on
AVPlayer
for some seconds?
当我们暂停
AVPlayer
通过时[player pause]
,是AVPlayer
继续缓冲来自网络的视频还是只是停止?我无法在苹果的文档中获得任何与此相关的信息。此外,是否可以强制 AVPlayer 在暂停时保持缓冲,以便如果我们暂停的视频正在等待第一个视频结束,那么我们不会在视频之间发现任何间隙?在暂停时,
AVPlayer
我们在 上有任何事件[player pause]
。我们可以显示
AVPlayer
几秒钟的静止图像吗?
Thanks
谢谢
回答by djromero
1) AVPlayer
will 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.loadedTimeRanges
to 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 currentItem
properties that may help you: playbackLikelyToKeepUp
, playbackBufferFull
and playbackBufferEmpty
.
此外,还有一些其他currentItem
属性可以帮助您:playbackLikelyToKeepUp
,playbackBufferFull
和playbackBufferEmpty
。
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 UIImageView
on 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.
示例项目:随意使用我编写的代码来支持我的答案。