详解iOS视频播放方式

2020-01-21 03:12:52于海丽


// 1、即将开始画中画
- (void)playerViewControllerWillStartPictureInPicture:(AVPlayerViewController *)playerViewController;
// 2、开始画中画
- (void)playerViewControllerDidStartPictureInPicture:(AVPlayerViewController *)playerViewController;
// 3、画中画失败
- (void)playerViewController:(AVPlayerViewController *)playerViewController failedToStartPictureInPictureWithError:(NSError *)error;
// 4、即将结束画中画
- (void)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController *)playerViewController;
// 5、结束画中画
- (void)playerViewControllerDidStopPictureInPicture:(AVPlayerViewController *)playerViewController;

 

 

我们看一个简单的Demo 

我们先不说关于AVFoundation复杂的东西,因为自己也是在学习这个 AVFoundation当中,我们先看一些很简单的Demo,就简单的利用一下AVFoundation 播放一下视频:

iOS,视频播放方式

我们在简单的看一下我们写的这部分的代码,简单的先使用了一下我们说的上面的一些知识点:


- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view.
 self.view.backgroundColor = [UIColor whiteColor]; 
 self.avPlayerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:MovieURL]];
 self.avPlayer = [[AVPlayer alloc]initWithPlayerItem:self.avPlayerItem];
 self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
 self.avPlayerLayer.frame = CGRectMake(10, 100, 355, 200);
 [self.view.layer addSublayer:self.avPlayerLayer];
 // 添加观察者
 [self addObserverWithAVPlayerItem];
}
#pragma mark --
#pragma mark -- KVO
-(void)addObserverWithAVPlayerItem{
 //状态添加观察者
 [self.avPlayerItem addObserver:self forKeyPath:@"status" options:(NSKeyValueObservingOptionNew) context:nil];
 // 缓存进度添加观察者
 [self.avPlayerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
 AVPlayerItem * avplayeritem = (AVPlayerItem *)object;
 if ([keyPath isEqualToString:@"status"]) {
 AVPlayerStatus status = [[change objectForKey:@"new"] intValue];
 if (status == AVPlayerStatusReadyToPlay) {
  NSLog(@"准备好播放");
  CMTime duration = avplayeritem.duration;
  NSLog(@"视频总时长:%.2f",CMTimeGetSeconds(duration));
  // 播放
  [self.avPlayer play];
 }else if (status == AVPlayerStatusFailed){
  NSLog(@"视频准备发生错误");
 }else{
  NSLog(@"位置错误");
 }
 }else if ([keyPath isEqualToString:@"loadedTimeRanges"]){
  // 可以自定义缓存进度
  NSTimeInterval timeInterval = [self alreadyCacheVideoProgress];
  NSLog(@"视频已经缓存的时长:%.2f",timeInterval);
 }
}

#pragma mark --
#pragma mark -- alreadyCacheVideoProgress
-(NSTimeInterval)alreadyCacheVideoProgress{
 
 // 先获取到它的缓存的进度
 NSArray * cacheVideoTime = [self.avPlayerItem loadedTimeRanges];
 // CMTimeRange 结构体 start duration 表示起始位置 和 持续时间
 // 获取缓冲区域
 CMTimeRange timeRange = [cacheVideoTime.firstObject CMTimeRangeValue];
 float startSeconds = CMTimeGetSeconds(timeRange.start);
 float durationSeconds = CMTimeGetSeconds(timeRange.duration);
 // 计算总缓冲时间 = start + duration
 NSTimeInterval result = startSeconds + durationSeconds;
 return result;
}