ios - AVPlayerItem 无法监听到播放卡顿:AVPlayerItemPlaybackStalledNotification
PHP中文网
PHP中文网 2017-04-18 09:05:17
0
0
1028
  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讲师

membalas semua(0)
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!