目的:想要监听播放器的卡顿情况
播放器初始化及监听代码:
// init player:
AVURLAsset* asset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem* _playerItem = [AVPlayerItem playerItemWithAsset:asset];
[self addObserverForCurrentPlayItem: _playerItem];
if(!_player)
{
_player = [AVPlayer playerWithPlayerItem:_playerItem];
}
// 监听代码
- (void)addObserverForCurrentPlayItem:(AVPlayerItem*)currentPlayerItem
{
[currentPlayerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
[currentPlayerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
[currentPlayerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
[currentPlayerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onPlayBuffering)
name:AVPlayerItemPlaybackStalledNotification
object:currentPlayerItem];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onPlayFinished)
name:AVPlayerItemDidPlayToEndTimeNotification
object:currentPlayerItem];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onPlayFailed)
name:AVPlayerItemFailedToPlayToEndTimeNotification
object:currentPlayerItem];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteChanged:)
name:AVAudioSessionRouteChangeNotification object:nil];
}
//回调函数
- (void)onPlayBuffering
{
NSLog(@"-> onPlayBuffering\n");
[delegate onPlayerDidBufferingPlayingVideo];
}
问题:为什么对网络进行限速,或拖到卡时,onPlayBuffering方法总是不会被回调?
认证高级PHP讲师