ios - AVPlayerItem 无法监听到播放卡顿:AVPlayerItemPlaybackStalledNotification
PHP中文网
PHP中文网 2017-04-18 09:05:17
0
0
1084
  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方法总是不会被回调?

PHP中文网
PHP中文网

认证高级PHP讲师

모든 응답(0)
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!