在ios9的时候苹果推出了AVPlayerViewController来代替MPMoviePlayerController,其的基本使用如下所示
有的时候我们想要通过把view加在控制器的view中,我们就可以自己去指定它的frame
//1、获取URL地址
NSURL * url = [[NSBundle mainBundle]URLForResource:@"test.mp4" withExtension:nil];
//2、AV播放视图控制器
AVPlayerViewController * pVC = [AVPlayerViewController new];
//3、创建player
pVC.player = [AVPlayer playerWithURL:url];
//4、准备播放
[pVC.player play];
//5、模态视图的弹出
//[self presentViewController:pVC animated:YES completion:nil];
pVC.view.frame = CGRectMake(0, 0, 200, 200);
[self.view addSubview:pVC.view];
接下来再简单的介绍下如何获取某一秒下视频的截图
首先我们要去明确下概念CMTime的概念,其中这里的Times的意思就是影片时间的意思
Value的意思就是帧数的意思,timeScale表示视频的每秒的帧数,就比如这里算时间就是60/1 为60s,所以得到的就是视频60s的时候的图像。
CMTime time = CMTimeMake(60, 1);
具体代码如下所示
//1、创建URL
NSURL * url = [[NSBundle mainBundle]URLForResource:@"test.mp4" withExtension:nil];
//2、获取资源
AVAsset * asset = [AVAsset assetWithURL:url];
//3、创建资源图像生成器
AVAssetImageGenerator * imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
//4、开始生成图像
//Times表示影片的时间的值
CMTime time = CMTimeMake(60, 1);
NSValue * value = [NSValue valueWithCMTime:time];
[imageGenerator generateCGImagesAsynchronouslyForTimes:@[value] completionHandler:^(CMTime requestedTime, CGImageRef
_Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) {
//在这里面是子线程来执行的
//5、在主线程中更新UI
dispatch_sync(dispatch_get_main_queue(), ^{
self.imageView.image = [UIImage imageWithCGImage:image];
});
}];