如何处理播放器是否正在缓冲并在目标c中显示加载通知

时间:2019-05-15 12:56:44

标签: ios objective-c observable avplayer

所以我在处理AVPlayer的缓冲和显示通知时遇到问题

我尝试将NSSNortificationCenter与addObserved一起使用并手动添加观察(您可以在我的代码中看到它),但是它不起作用。所以我需要的是在缓冲区加载时出现通知。期待您的帮助

-(void)initPlayer{

    NSURL *url = [NSURL URLWithString:@"https://devstreaming-cdn.apple.com/videos/streaming/examples/img_bipbop_adv_example_ts/master.m3u8"];
    _asset = [AVAsset assetWithURL:url];
    _player = [[AVPlayer alloc] initWithPlayerItem:[[AVPlayerItem alloc] initWithAsset:_asset]];
    [_player addObserver:self forKeyPath:@"path" options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial context:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(makeNortification:) name:AVPlayerItemPlaybackStalledNotification object:_player];
    _layer = [AVPlayerLayer playerLayerWithPlayer:_player];
    [_layer setFrame:(self.view.frame)];
    [self.view.layer addSublayer:_layer];
    [_player play];
    [self setSliderMaxValue];
-(void)makeNortification:(NSNotification *) nortification{
    NSLog(@"waiting..");
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if (object == _player &&[keyPath isEqualToString:@"status"] ){
        if(_player.status == AVPlayerStatusReadyToPlay){
            NSLog(@"player is ready");
    }
        else if(_player.status == AVPlayerStatusUnknown || _player.status == AVPlayerStatusFailed){
            NSLog(@"waiting.");

        }
        else{

    }
}

0 个答案:

没有答案