AVPlayerItem 无法监听到播放卡顿:AVPlayerItemPlaybackStalledNotification

  1. 目的:想要监听播放器的卡顿情况

  2. 播放器初始化及监听代码:

// 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方法总是不会被回调?

阅读 7.3k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题