iOS 自定义录制视频功能

转自:http://blog.****.net/lwjok2007/article/details/50925336

大家都知道 iOS提供了一个系统录制使用的方法

UIImagePcikerController 他不仅有拍照的功能 还有录制视屏的功能。这个方法大家自己研究一下,我们今天主要讲一下自定义 录制视屏的方法。

我们使用AVCaptureSession 来自定义录制视屏的界面

大致过程如下

1,建立Session  

2,添加input

3,添加output

4,开始捕捉

5,为用户显示当前录制状态

6,结束捕捉

7,结束捕捉


好了 我们开始写代码

创建项目

iOS 自定义录制视频功能


iOS 自定义录制视频功能


添加AVFoundation.framework 

iOS 自定义录制视频功能


iOS 自定义录制视频功能


接下来 我们在 默认生成的 VIewController中写代码

首先导入 AVFoundation

[objc] view plain copy
  1. #import <AVFoundation/AVFoundation.h>  

创建一个UIView 用来显示录像内容

[objc] view plain copy
  1. UIView *userCamera;  


创建几个 相关的属性

[objc] view plain copy
  1. @property (strong,nonatomicAVCaptureSession *captureSession;//负责输入和输出设置之间的数据传递  
  2. @property (strong,nonatomicAVCaptureDeviceInput *captureDeviceInput;//负责从AVCaptureDevice获得输入数据  
  3. @property (strong,nonatomicAVCaptureMovieFileOutput *captureMovieFileOutput;//视频输出流  
  4. @property (strong,nonatomicAVCaptureVideoPreviewLayer *captureVideoPreviewLayer;//相机拍摄预览图层  

首先,在ViewDidLoad中创建userCamera

用来显示录像结果

[objc] view plain copy
  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.     // Do any additional setup after loading the view, typically from a nib.  
  4.     userCamera=[[UIView alloc]initWithFrame:CGRectMake(064, screen_Width, screen_Height-70)];  
  5.     userCamera.backgroundColor=[UIColor grayColor];  
  6.     [self.view addSubview:userCamera];  
  7.       
  8. }  

我们在viewWillAppear 方法中创建录像相关的功能

[objc] view plain copy
  1. - (void)viewWillAppear:(BOOL)animated{  
  2.     [super viewWillAppear:animated];  
  3.     //初始化会话  
  4.     _captureSession=[[AVCaptureSession alloc]init];  
  5.     if ([_captureSession canSetSessionPreset:AVCaptureSessionPreset1280x720]) {//设置分辨率  
  6.         _captureSession.sessionPreset=AVCaptureSessionPreset1280x720;  
  7.     }  
  8.       
  9.     //获得输入设备  
  10.     AVCaptureDevice *captureDevice=[self getCameraDeviceWithPosition:AVCaptureDevicePositionBack];//取得后置摄像头  
  11.     if (!captureDevice) {  
  12.         NSLog(@"取得后置摄像头时出现问题.");  
  13.         return;  
  14.     }  
  15.     //添加一个音频输入设备  
  16.     AVCaptureDevice *audioCaptureDevice=[[AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio] firstObject];  
  17.       
  18.       
  19.     NSError *error=nil;  
  20.     //根据输入设备初始化设备输入对象,用于获得输入数据  
  21.     _captureDeviceInput=[[AVCaptureDeviceInput alloc]initWithDevice:captureDevice error:&error];  
  22.     if (error) {  
  23.         NSLog(@"取得设备输入对象时出错,错误原因:%@",error.localizedDescription);  
  24.         return;  
  25.     }  
  26.     AVCaptureDeviceInput *audioCaptureDeviceInput=[[AVCaptureDeviceInput alloc]initWithDevice:audioCaptureDevice error:&error];  
  27.     if (error) {  
  28.         NSLog(@"取得设备输入对象时出错,错误原因:%@",error.localizedDescription);  
  29.         return;  
  30.     }  
  31.       
  32.     //初始化设备输出对象,用于获得输出数据  
  33.     _captureMovieFileOutput=[[AVCaptureMovieFileOutput alloc]init];  
  34.       
  35.     //将设备输入添加到会话中  
  36.     if ([_captureSession canAddInput:_captureDeviceInput]) {  
  37.         [_captureSession addInput:_captureDeviceInput];  
  38.         [_captureSession addInput:audioCaptureDeviceInput];  
  39.         AVCaptureConnection *captureConnection=[_captureMovieFileOutput connectionWithMediaType:AVMediaTypeVideo];  
  40.         if ([captureConnection isVideoStabilizationSupported ]) {  
  41.             captureConnection.preferredVideoStabilizationMode=AVCaptureVideoStabilizationModeAuto;  
  42.         }  
  43.     }  
  44.       
  45.     //将设备输出添加到会话中  
  46.     if ([_captureSession canAddOutput:_captureMovieFileOutput]) {  
  47.         [_captureSession addOutput:_captureMovieFileOutput];  
  48.     }  
  49.   
  50.       
  51.     //创建视频预览层,用于实时展示摄像头状态  
  52.     _captureVideoPreviewLayer=[[AVCaptureVideoPreviewLayer alloc]initWithSession:self.captureSession];  
  53.       
  54.     CALayer *layer=userCamera.layer;  
  55.     layer.masksToBounds=YES;  
  56.       
  57.     _captureVideoPreviewLayer.frame=layer.bounds;  
  58.     _captureVideoPreviewLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;//填充模式  
  59.     //将视频预览层添加到界面中  
  60.     [layer addSublayer:_captureVideoPreviewLayer];  
  61.       
  62.       
  63.       
  64.       
  65. }  

添加用到的方法

[objc] view plain copy
  1. /** 
  2.  *  捕获区域改变 
  3.  * 
  4.  *  @param notification 通知对象 
  5.  */  
  6. -(void)areaChange:(NSNotification *)notification{  
  7.     NSLog(@"捕获区域改变...");  
  8. }  
  9. #pragma mark - 私有方法  
  10.   
  11. /** 
  12.  *  取得指定位置的摄像头 
  13.  * 
  14.  *  @param position 摄像头位置 
  15.  * 
  16.  *  @return 摄像头设备 
  17.  */  
  18. -(AVCaptureDevice *)getCameraDeviceWithPosition:(AVCaptureDevicePosition )position{  
  19.     NSArray *cameras= [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];  
  20.     for (AVCaptureDevice *camera in cameras) {  
  21.         if ([camera position]==position) {  
  22.             return camera;  
  23.         }  
  24.     }  
  25.     return nil;  
  26. }  


在viewDidAppear 中开启录像

在viewDidDisappear中关闭录像

[objc] view plain copy
  1. -(void)viewDidAppear:(BOOL)animated{  
  2.     [super viewDidAppear:animated];  
  3.     [self.captureSession startRunning];  
  4. }  
  5.   
  6. -(void)viewDidDisappear:(BOOL)animated{  
  7.     [super viewDidDisappear:animated];  
  8.     [self.captureSession stopRunning];  
  9. }  


好了 我们试试看结果

iOS 自定义录制视频功能


好了 ,代码我会上传到群空间 

大家有兴趣可以去下载

demo:【60318自定义录像AVCapture1.zip】

此处demo很简单 更为丰富的功能大家可以加群一起讨论

苹果开发群 :414319235  欢迎加入,共同学习