avplayer

参考
1.iOS通过AVPlayer打造自己的视频播放器
2.基于 AVPlayer 自定义播放器
3.AVPlayer的基本使用

        if (_player) {
            return;
        }
        _playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:imageModel.videoPath]];
        _player = [AVPlayer playerWithPlayerItem:_playerItem];
        _playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
        _playerLayer.frame = gesture.view.bounds;
        [gesture.view.layer addSublayer:_playerLayer];
        [_player play];
        
        //监听当视频播放结束时
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:[self.player currentItem]];
        //    //监听播放失败时
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playbackFailed:) name:AVPlayerItemFailedToPlayToEndTimeNotification object:[self.player currentItem]];
- (void)playbackFinished:(NSNotification *)notification {
    AVPlayerItem *item = [notification object];
    [item seekToTime:kCMTimeZero]; // item 跳转到初始
    if (item == _playerItem) {
        [self resetPlayer];
    }
}
- (void)resetPlayer {
    if (_playerLayer) {
        [_player pause];
        [_playerLayer removeFromSuperlayer];
        [_player replaceCurrentItemWithPlayerItem:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        _playerLayer = nil;
        _playerItem = nil;
        _player = nil;
    }
}
- (void)playbackFailed:(NSNotification *)notification {
    AVPlayerItem *item = [notification object];
    [item seekToTime:kCMTimeZero]; // item 跳转到初始
    if (item == _playerItem) {
        [self resetPlayer];
    }
}

KVO 获取视频信息, 观察缓冲进度

观察 AVPlayerItem 的 status 属性,当状态变为 AVPlayerStatusReadyToPlay 时才可以使用。
也可以观察 loadedTimeRanges 获取缓冲进度

注册观察者
[_playerItem addObserver:self forKeyPath:@"status" options:(NSKeyValueObservingOptionNew) context:nil]; // 观察status属性
监听观察者

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    AVPlayerItem *item = (AVPlayerItem *)object;
    if ([keyPath isEqualToString:@"status"]) {
            AVPlayerStatus status = [[change objectForKey:@"new"] intValue]; // 获取更改后的状态
            if (status == AVPlayerStatusReadyToPlay) {
                CMTime duration = item.duration; // 获取视频长度
                // 设置视频时间
                [self setMaxDuration:CMTimeGetSeconds(duration)];
                // 播放
                [self play];
            } else if (status == AVPlayerStatusFailed) {
                NSLog(@"AVPlayerStatusFailed");
            } else {
                NSLog(@"AVPlayerStatusUnknown");
            }

    } else if ([keyPath isEqualToString:@"loadedTimeRanges"]) {
        NSTimeInterval timeInterval = [self availableDurationRanges]; // 缓冲时间
        CGFloat totalDuration = CMTimeGetSeconds(_playerItem.duration); // 总时间
        [self.loadedProgress setProgress:timeInterval / totalDuration animated:YES]; // 更新缓冲条
    }
}

你可能感兴趣的:(avplayer)