1.首先对playerItem 进行监听
[playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
[playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];
2.在监听方法里
- (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) {
// 做一些缓冲的操作 例如展示label 或者 加载一个缓冲条 playBufferEmpty (用于标识是否处于缓冲状态)
playBufferEmpty = TRUE;
[indicator startAnimating];
[vidStreaminglabel setText:@"Buffering..."];
[vidStreaminglabel setHidden:NO];
}
}
else if (object == playerItem && [keyPath isEqualToString:@"playbackLikelyToKeepUp"])
{
if (playerItem.playbackLikelyToKeepUp)
{
//做一些结束缓冲的操作
playBufferEmpty = FALSE;
[indicator stopAnimating];
[vidStreaminglabel setHidden:YES];
}
}
}