由于MPMoviePlayerController与MPMoviePlayerViewController在iOS9.0之后被弃用,所以采用AVPlayer以及AVPlayerViewController来实现基础的视频播放。
AVPlayerViewController
这个是苹果给我们封装好的了、使用也非常简单、但是界面貌似没发自定义、适合没有界面要求的需求。但是简单啊!简单啊!
废话不说、直接上代码了解一下:
//步骤1:获取视频路径
NSString *webVideoPath = @"https://vd2.bdstatic.com/mda-kidkfudrpqgg8891/sc/cae_h264_clips/mda-kidkfudrpqgg8891.mp4";
NSURL*webVideoUrl = [NSURLURLWithString:webVideoPath];
//步骤2:创建AVPlayer
AVPlayer * avPlayer = [[AVPlayer alloc]initWithURL:webVideoUrl];
//步骤3:使用AVPlayer创建AVPlayerViewController,并跳转播放界面
AVPlayerViewController *avPlayerVC =[[AVPlayerViewController alloc] init];
avPlayerVC.player= avPlayer;
[self presentViewController:avPlayerVC animated:YES completion:nil];
没错、已经结束了!开心的用起来吧!😂
AVPlayer
对于AVPlayer来说就相对麻烦一点了、相对谁?相对AVPlayerViewController!!可见也不怎么麻烦对吧😂。
首先新建一个普普通通的viewController,导入头文件、创建对象
#import <AVFoundation/AVFoundation.h>
@property (nonatomic ,strong) AVPlayer * avplayer;
@property (nonatomic ,strong) AVPlayerItem * avplayerItem;
@property (nonatomic ,strong) AVPlayerLayer * playLayer;
初始化相关对象和数据设置
//本地视频
// NSString * localPath = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"mp4"];
// NSURL * localUrl = [NSURL fileURLWithPath:localPath];
//网络视频
NSString * webPath = @"https://vd2.bdstatic.com/mda-kidkfudrpqgg8891/sc/cae_h264_clips/mda-kidkfudrpqgg8891.mp4";
NSURL* webUrl = [NSURLURLWithString:webPath];
//初始化播放器
self.avplayerItem = [[AVPlayerItem alloc] initWithURL:webUrl];
self.avplayer = [[AVPlayer alloc] initWithPlayerItem:self.avplayerItem];
//创建显示视频的AVPLayerLayer
/*
AVLayerVideoGravityResizeAspectFill等比例铺满,宽或高有可能出屏幕
AVLayerVideoGravityResizeAspect 等比例 默认
AVLayerVideoGravityResize 完全适应宽高
*/
_playLayer = [AVPlayerLayer playerLayerWithPlayer:self.avplayer];
_playLayer.videoGravity = AVLayerVideoGravityResizeAspect;
_playLayer.frame = CGRectMake(0, 100, DEVICEWIDTH, DEVICEHEIGHT/2);
[self.view.layer addSublayer:_playLayer];
//开始播放
[self.avplayer play];
这样一个简单的视频播放就完成了!但是、听好了但是,这才完成了3分之一而已、剩下的该做什么呢?
暂停?
关于视频的暂停播放、很简单
[self.avplayerpause];
结束了!
继续播放?
那就
[self.avplayer play];
what?就这样?
对啊、就是这么简单啊!
播放监听?
视频播放肯定有自己的状态、是否缓冲完毕、加载失败、异常、视频长度、播放进度等情况。那么该如何监听处理呢?
利用KVO给AVPlayer的currentItem添加属性监听、监听“status”和“loadedTimeRanges”关键字属性。
[self.avplayer.currentItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
调用属性监听方法
//属性监听
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void*)context
{
AVPlayerItem* playitem = (AVPlayerItem*)object;
if([keyPathisEqualToString:@"status"]) {
AVPlayerStatus status = [[change objectForKey:@"new"] intValue];
switch(status) {
case AVPlayerStatusReadyToPlay:{ //准备就绪
//获取视频时间
CMTimeduration = playitem.duration;
NSString* time = [selfformatTimeWithTimeInterVal:CMTimeGetSeconds(duration)];
//显示视频总长度 自定义一个label就可以
self.timeLabel.text= time;
//开启滑块 为了控制播放进度
self.sliderView.enabled=YES;
[self.avplayer play];
break;
}
case AVPlayerStatusFailed:{ //失败、可以尝试重试
break;
}
case AVPlayerStatusUnknown:{ //异常
break;
}
default:
break;
}
}
}
通过上边的方法就可以获取到视频的状态和长度、那么接下来就要对播放过程进行处理。
__weak typeof(self) weakSelf = self;
self.timeObserver = [self.avplayer addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
//当前播放时间
NSTimeInterval nowTime = CMTimeGetSeconds(time);
//总时间
NSTimeInterval totaltime = CMTimeGetSeconds(weakSelf.avplayer.currentItem.duration);
//滑块进度
weakSelf.sliderView.value= nowTime/totaltime;
//倒计时
weakSelf.nowtimeLabel.text= [weakSelfformatTimeWithTimeInterVal:nowTime];
}];
上边这一段代码就可以动态的显示播放的时间、和播放的进度,具体显示的样式以个人实际情况而定!
好了、先到这、后期有需求接着完善!
网友评论