我的Demo地址---------> 点击打开链接
#pragma mark - 添加进度观察 - addProgressObserver
- (void)addProgressObserver {
// 设置每秒执行一次
[_playerHelper.getAVPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue: NULL usingBlock:^(CMTime time) {
NSLog(@"进度观察 + %f", _topProgressSlider.value);
// 获取当前时间
CMTime currentTime = _playerHelper.getAVPlayer.currentItem.currentTime;
// 转化成秒数
CGFloat currentPlayTime = (CGFloat)currentTime.value / currentTime.timescale;
// 总时间
CMTime totalTime = playItem.duration;
// 转化成秒
_totalMovieDuration = (CGFloat)totalTime.value / totalTime.timescale;
// 相减后
_topProgressSlider.value = CMTimeGetSeconds(currentTime) / _totalMovieDuration;
progressSlider = CMTimeGetSeconds(currentTime) / _totalMovieDuration;
NSLog(@"%f", _topProgressSlider.value);
NSDate *pastDate = [NSDate dateWithTimeIntervalSince1970: currentPlayTime];
_topPastTimeLabel.text = [self getTimeByDate:pastDate byProgress: currentPlayTime];
CGFloat remainderTime = _totalMovieDuration - currentPlayTime;
NSDate *remainderDate = [NSDate dateWithTimeIntervalSince1970: remainderTime];
_topRemainderLabel.text = [self getTimeByDate:remainderDate byProgress: remainderTime];
if (_isFirstRotatorTap) {
[self setTopRightBottomViewShowToHidden];
} else {
[self setTopRightBottomViewHiddenToShow];
}
}];
// 设置topProgressSlider图片
UIImage *thumbImage = [UIImage imageNamed:@"slider-metal-handle.png"];
[self.topProgressSlider setThumbImage:thumbImage forState:UIControlStateHighlighted];
[self.topProgressSlider setThumbImage:thumbImage forState:UIControlStateNormal];
}
- (void)addAVPlayer{
playItem = [AVPlayerItem playerItemWithURL: self.movieURL];
self.playerHelper = [[ChuLjMovieHelper alloc] init];
[_playerHelper initAVPlayerWithAVPlayerItem:playItem];
// 创建显示层
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer: _playerHelper.getAVPlayer];
_playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
// 竖屏的时候frame
[self setVerticalFrame];
// 这是视频的填充模式, 默认为AVLayerVideoGravityResizeAspect
_playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
// 插入到view层上面, 没有用addSubLayer
[self.view.layer insertSublayer:_playerLayer atIndex:0];
// 添加进度观察
[self addProgressObserver];
[self addObserverToPlayerItem: playItem];
}
// 观察者的方法, 会在加载好后触发, 可以在这个方法中, 保存总文件的大小, 用于后面的进度的实现
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
AVPlayerItem *playerItem = (AVPlayerItem *)object;
if ([keyPath isEqualToString:@"status"]) {
AVPlayerStatus status = [[change objectForKey:@"new"] intValue];
if (status == AVPlayerStatusReadyToPlay) {
NSLog(@"正在播放...,视频总长度: %.2f",CMTimeGetSeconds(playerItem.duration));
CMTime totalTime = playerItem.duration;
self.totalMovieDuration = (CGFloat)totalTime.value / totalTime.timescale;
}
}
if ([keyPath isEqualToString:@"loadedTimeRanges"]) {
NSArray *array = playerItem.loadedTimeRanges;
// 本次缓冲时间范围
CMTimeRange timeRange = [array.firstObject CMTimeRangeValue];
float startSeconds = CMTimeGetSeconds(timeRange.start);
float durationSeconds = CMTimeGetSeconds(timeRange.duration);
// 缓冲总长度
NSTimeInterval totalBuffer = startSeconds + durationSeconds;
// NSLog(@"共缓冲%.2f", totalBuffer);
NSLog(@"进度 + %f", progressSlider);
self.topProgressSlider.value = progressSlider;
}
}
相关知识,demo有注释,不明白的地方欢迎留言。