iOS-自定义视频播放器

采用调用苹果自带的AVPlayer来实现,但还是支持大部分格式的视频
主要功能:
1.视频播放,暂停
2.视频缓冲,视频播放状态
3.监听播放进度
4.监听播放完成

一.播放视频

1.将网络或者本地视频转化为NSURL
2.加载到播放器

   self.playerItem = [AVPlayerItem playerItemWithURL:url];
   self.player = [AVPlayer playerWithPlayerItem:self.playerItem];

3.创建播放视图

AVPlayerLayer *playerLayer=[AVPlayerLayer playerLayerWithPlayer:self.player];
        playerLayer.frame = view.bounds;
        playerLayer.videoGravity=AVLayerVideoGravityResizeAspect;//视频填充模式
        [view.layer addSublayer:playerLayer];

4.调用播放/暂停方法

- (void)start {
     [self.player play];
}

- (void)pause {
    [self.player pause];
}

二.视频缓冲,视频播放状态

1.通过kvo监听AVPlayerItem的属性status和loadedTimeRanges

    [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:&PlayerItemStatusContext];
    //监控网络加载情况属性
    [playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:&PlayerItemLoadedTimeRangesContext];

2.根据属性的改变判断做相应处理
AVPlayerItemStatusReadyToPlay为正常播放状态

3.loadedTimeRanges属性就是加载的数据可以用来计算缓冲进度

NSTimeInterval timeInterval = [self availableDuration];// 计算缓冲进度
        CMTime duration = self.playerItem.duration;
        CGFloat totalDuration = CMTimeGetSeconds(duration);
        //缓冲进度------------
        self.toolBarView.playerProgress.progress = timeInterval / totalDuration;
//时间算法
- (NSTimeInterval)availableDuration {
    NSArray *loadedTimeRanges = [[_player currentItem] loadedTimeRanges];
    CMTimeRange timeRange = [loadedTimeRanges.firstObject CMTimeRangeValue];// 获取缓冲区域
    float startSeconds = CMTimeGetSeconds(timeRange.start);
    float durationSeconds = CMTimeGetSeconds(timeRange.duration);
    NSTimeInterval result = startSeconds + durationSeconds;// 计算缓冲总进度
    return result;
}

三.监听播放进度

使用NSTimer执行updateProgressInfo方法

- (void)updateProgressInfo{
    if (!self.isPlaying) return;
    // 1.更新时间
    self.toolBarView.timeLabel.text = [self updateTimeString];
    double currentTime = CMTimeGetSeconds(self.player.currentTime);
    // 2.设置进度条的value
    self.toolBarView.playerSlider.value = currentTime;
}
#pragma mark - 讲当前时间转成时间戳给label显示
- (NSString*)updateTimeString
{
    NSTimeInterval currentTime = CMTimeGetSeconds(self.player.currentItem.currentTime);
    NSTimeInterval duration = CMTimeGetSeconds(self.player.currentItem.duration);
    return [self stringWithCurrentTime:currentTime duration:duration];
}
- (NSString *)stringWithCurrentTime:(NSTimeInterval)currentTime duration:(NSTimeInterval)duration
{
    NSInteger dMin = duration / 60;
    NSInteger dSec = (NSInteger)duration % 60;
    NSInteger cMin = currentTime / 60;
    NSInteger cSec = (NSInteger)currentTime % 60;
    NSString *durationString = [NSString stringWithFormat:@"%02ld:%02ld", dMin, dSec];
    NSString *currentString = [NSString stringWithFormat:@"%02ld:%02ld", cMin, cSec];
    return [NSString stringWithFormat:@"%@/%@", currentString, durationString];
}

四.监听播放完成

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerEndPlay:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.playerItem];

Demo下载

https://pan.baidu.com/s/1i5FInu1

你可能感兴趣的:(iOS-自定义视频播放器)