iOS -AVPlayer播放多个小视频切换

前言###

本文是上文的续集,如果要了解苹果系统的三种播放器以及AVPlayer播放工具类的封装、播放单个视频请移步到
三种视频播放器总结(播放多个小视频)

播放多个小视频的源码资源
项目下载地址:三种视频播放器大总结
项目效果图

三种播放器大总结.gif

怎样实现播放多个视频?####

demo需求:
1、播放多个视频(全屏播放)
2、播放完成自动切换到下一个视频
3、关闭任意一个视频就退出播放器
4、app进入后台暂停播放,进入前台继续播放
5、跳转到其他页面不再播放

具体问题分析:
1、要播放多个视频(全屏播放)并且关闭任意一个视频就退出播放器,那么可以设置一个播放控制器。如果这个控制器要播放多个视频,并且是横向切换,那么可以采用UICollectView
2、采用UICollectView有几个问题:A、当cell离开屏幕的时候,要停止播放器;B、要分别处理是左边滑动还是右边滑动;C、cell可能存在循环利用的问题?
3、刚进入页面的时候加载cell,播放器的播放预览层必须放在顶层

具体实现####

注意:UICollectView的数据源方法是在加载完UICollectView之后调用,所以我们第一次加载视频要在下面方法中做

  - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath

第一步
第一次加载播放器要在cell即将显示的时候手动调用

 - (void)playFirstCellWithFirst:(LZBVideoPlayCollectionViewCell *)firstCell
 {
if(firstCell == nil) return;
if(firstCell.videoPath.length == 0) return;
self.playingCell = firstCell;
self.currentVideoPath =firstCell.videoPath;
NSURL *url = [NSURL URLWithString:firstCell.videoPath];
//播放当前cell的视频
[self startPlayerWithUrl:url coverImageUrl:@"设置背景图片" playerSuperView:firstCell.contentView];  
  }

播放当前的视频地址

//开始播放
- (void)startPlayerWithUrl:(NSURL *)url coverImageUrl:(NSString *)coverUrl playerSuperView:(UIView *)superView
{
_weak __typeof(self) weakSelf = self;
[[LZBVideoPlayer sharedInstance] playVideoUrl:url coverImageurl:coverUrl showInSuperView:superView];
[LZBVideoPlayer sharedInstance].openSoundWhenPlaying = YES;
//剩余时间
[[LZBVideoPlayer sharedInstance] setPlayerTimeProgressBlock:^(long residueTime) {
    [weakSelf.playingCell reloadTimeLabelWithTime:residueTime];
    if(residueTime == 0)
    {
        //播放完成,自动滚动到下一页
        [weakSelf scrollToNextCell];
   }];
 }

第二步
在下一个cell滚动到中心位置的时候,获取当前屏幕的cell,此时应该有两个cell,停止播放上一个视频,开始播放下一个cell的视频

 //获取当前屏幕的cell
NSArray *visiableCells = [self.collectionView visibleCells];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
LZBVideoPlayCollectionViewCell  *nextCell = (LZBVideoPlayCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
if([self.playingCell isEqual:nextCell]) return;
if(nextCell == nil) return;
//屏幕到中间时候,停止上一个cell视频播放
if ([visiableCells containsObject:self.playingCell]) {
    [self stopPlayer];
}
//开始播放下一个视频
self.playingCell = nextCell;
self.currentVideoPath = nextCell.videoPath;
NSURL *url = [NSURL URLWithString:nextCell.videoPath];
[self startPlayerWithUrl:url coverImageUrl:@"设置背景图片" playerSuperView:nextCell.contentView];

第三步
监听app进入后台或者前台,使用通知

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground) name:UIApplicationWillResignActiveNotification object:nil];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterPlayGround) name:UIApplicationDidBecomeActiveNotification object:nil];
 //app进入后台
- (void)appDidEnterBackground
{
 if(self.stopWhenAppDidEnterBackground)
 {
   [self pause];  //暂停播放
 }
}
//app进入播放前台
- (void)appDidEnterPlayGround
 {
   [self playWithResume];  //继续播放
}

其中有一些细节的处理,请查看demo
项目下载地址:三种视频播放器大总结
项目效果图

最后赠言###

如果觉得文章对您有帮助,不要忘记star哦!,star 是对程序猿最大的鼓励!

你可能感兴趣的:(iOS -AVPlayer播放多个小视频切换)