iOS 自定义录制视频功能
转自:http://blog.****.net/lwjok2007/article/details/50925336
大家都知道 iOS提供了一个系统录制使用的方法
UIImagePcikerController 他不仅有拍照的功能 还有录制视屏的功能。这个方法大家自己研究一下,我们今天主要讲一下自定义 录制视屏的方法。
我们使用AVCaptureSession 来自定义录制视屏的界面
大致过程如下
1,建立Session
2,添加input
3,添加output
4,开始捕捉
5,为用户显示当前录制状态
6,结束捕捉
7,结束捕捉
好了 我们开始写代码
创建项目
添加AVFoundation.framework
接下来 我们在 默认生成的 VIewController中写代码
首先导入 AVFoundation
- #import <AVFoundation/AVFoundation.h>
创建一个UIView 用来显示录像内容
- UIView *userCamera;
- @property (strong,nonatomic) AVCaptureSession *captureSession;//负责输入和输出设置之间的数据传递
- @property (strong,nonatomic) AVCaptureDeviceInput *captureDeviceInput;//负责从AVCaptureDevice获得输入数据
- @property (strong,nonatomic) AVCaptureMovieFileOutput *captureMovieFileOutput;//视频输出流
- @property (strong,nonatomic) AVCaptureVideoPreviewLayer *captureVideoPreviewLayer;//相机拍摄预览图层
首先,在ViewDidLoad中创建userCamera
用来显示录像结果
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- userCamera=[[UIView alloc]initWithFrame:CGRectMake(0, 64, screen_Width, screen_Height-70)];
- userCamera.backgroundColor=[UIColor grayColor];
- [self.view addSubview:userCamera];
- }
我们在viewWillAppear 方法中创建录像相关的功能
- - (void)viewWillAppear:(BOOL)animated{
- [super viewWillAppear:animated];
- //初始化会话
- _captureSession=[[AVCaptureSession alloc]init];
- if ([_captureSession canSetSessionPreset:AVCaptureSessionPreset1280x720]) {//设置分辨率
- _captureSession.sessionPreset=AVCaptureSessionPreset1280x720;
- }
- //获得输入设备
- AVCaptureDevice *captureDevice=[self getCameraDeviceWithPosition:AVCaptureDevicePositionBack];//取得后置摄像头
- if (!captureDevice) {
- NSLog(@"取得后置摄像头时出现问题.");
- return;
- }
- //添加一个音频输入设备
- AVCaptureDevice *audioCaptureDevice=[[AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio] firstObject];
- NSError *error=nil;
- //根据输入设备初始化设备输入对象,用于获得输入数据
- _captureDeviceInput=[[AVCaptureDeviceInput alloc]initWithDevice:captureDevice error:&error];
- if (error) {
- NSLog(@"取得设备输入对象时出错,错误原因:%@",error.localizedDescription);
- return;
- }
- AVCaptureDeviceInput *audioCaptureDeviceInput=[[AVCaptureDeviceInput alloc]initWithDevice:audioCaptureDevice error:&error];
- if (error) {
- NSLog(@"取得设备输入对象时出错,错误原因:%@",error.localizedDescription);
- return;
- }
- //初始化设备输出对象,用于获得输出数据
- _captureMovieFileOutput=[[AVCaptureMovieFileOutput alloc]init];
- //将设备输入添加到会话中
- if ([_captureSession canAddInput:_captureDeviceInput]) {
- [_captureSession addInput:_captureDeviceInput];
- [_captureSession addInput:audioCaptureDeviceInput];
- AVCaptureConnection *captureConnection=[_captureMovieFileOutput connectionWithMediaType:AVMediaTypeVideo];
- if ([captureConnection isVideoStabilizationSupported ]) {
- captureConnection.preferredVideoStabilizationMode=AVCaptureVideoStabilizationModeAuto;
- }
- }
- //将设备输出添加到会话中
- if ([_captureSession canAddOutput:_captureMovieFileOutput]) {
- [_captureSession addOutput:_captureMovieFileOutput];
- }
- //创建视频预览层,用于实时展示摄像头状态
- _captureVideoPreviewLayer=[[AVCaptureVideoPreviewLayer alloc]initWithSession:self.captureSession];
- CALayer *layer=userCamera.layer;
- layer.masksToBounds=YES;
- _captureVideoPreviewLayer.frame=layer.bounds;
- _captureVideoPreviewLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;//填充模式
- //将视频预览层添加到界面中
- [layer addSublayer:_captureVideoPreviewLayer];
- }
添加用到的方法
- /**
- * 捕获区域改变
- *
- * @param notification 通知对象
- */
- -(void)areaChange:(NSNotification *)notification{
- NSLog(@"捕获区域改变...");
- }
- #pragma mark - 私有方法
- /**
- * 取得指定位置的摄像头
- *
- * @param position 摄像头位置
- *
- * @return 摄像头设备
- */
- -(AVCaptureDevice *)getCameraDeviceWithPosition:(AVCaptureDevicePosition )position{
- NSArray *cameras= [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
- for (AVCaptureDevice *camera in cameras) {
- if ([camera position]==position) {
- return camera;
- }
- }
- return nil;
- }
在viewDidAppear 中开启录像
在viewDidDisappear中关闭录像
- -(void)viewDidAppear:(BOOL)animated{
- [super viewDidAppear:animated];
- [self.captureSession startRunning];
- }
- -(void)viewDidDisappear:(BOOL)animated{
- [super viewDidDisappear:animated];
- [self.captureSession stopRunning];
- }
好了 我们试试看结果
好了 ,代码我会上传到群空间
大家有兴趣可以去下载
demo:【60318自定义录像AVCapture1.zip】
此处demo很简单 更为丰富的功能大家可以加群一起讨论
苹果开发群 :414319235 欢迎加入,共同学习