ios avplayer触发流式传输缓冲区外 [英] ios avplayer trigger streaming is out of buffer
问题描述
我要在流缓冲区为空时重新连接到服务器.
I want to reconnect to the server when the streaming buffer is empty.
AVPlayer
或AVPlayerItem
缓冲区为空时如何触发方法?
How can I trigger a method when the AVPlayer
or AVPlayerItem
buffer is empty?
我知道可以使用playbackLikelyToKeepUp
,playbackBufferEmpty
和playbackBufferFull
方法来检查缓冲区状态,但是这些不是回调.
I know there are playbackLikelyToKeepUp
, playbackBufferEmpty
and playbackBufferFull
methods to check the buffer status, but those are not callbacks.
是否有任何回调函数,或者我应该添加的观察者?
Are there any callback functions, or any observers I should add?
推荐答案
您可以为这些键添加观察者:
you can add observer for those keys:
[playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
[playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];
第一个在缓冲区为空时会警告您,第二个在缓冲区可以再次使用时警告您.
The first one will warn you when your buffer is empty and the second when your buffer is good to go again.
然后,您可以使用以下代码来处理密钥更改:
Then to handle the key change you can use this code:
- (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 avplayer触发流式传输缓冲区外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!