本文共 3837 字,大约阅读时间需要 12 分钟。
通过的学习,我们学会了AVCaptureSession的基本使用。现在,同样使用AVCaptureSession类,只需要做微小的修改就可以完成视频拍摄。
声明属性
首先,我们要声明拍摄所必须的属性:@property (nonatomic, strong) AVCaptureSession *captureSession;@property (nonatomic, strong) AVCaptureDevice *videoDevice;@property (nonatomic, strong) AVCaptureDevice *audioDevice;@property (nonatomic, strong) AVCaptureDeviceInput *videoInput;@property (nonatomic, strong) AVCaptureDeviceInput *audioInput;@property (nonatomic, strong) AVCaptureMovieFileOutput *movieFileOutput;@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;
因为视频会包含音频信息,所以比拍摄图片多出了音频设备(audioDevice)和音频输入(audioInput)。而我们的输出类由AVCaptureStillImageOutput变成了AVCaptureMovieFileOutput,他们同样都是AVCaptureOutput的子类。
捕捉会话初始化 在拍摄视频前,需要初始化捕捉会话(AVCaptureSession):- (void)setupCaptureSession { // 1.获取视频设备 NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; for (AVCaptureDevice *device in devices) { if (device.position == AVCaptureDevicePositionBack) { self.videoDevice = device; break; } } // 2.获取音频设备 self.audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio]; // 3.创建视频输入 NSError *error = nil; self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:self.videoDevice error:&error]; if (error) { return; } // 4.创建音频输入 self.audioInput = [AVCaptureDeviceInput deviceInputWithDevice:self.audioDevice error:&error]; if (error) { return; } // 5.创建视频输出 self.movieFileOutput = [[AVCaptureMovieFileOutput alloc] init]; // 6.建立会话 self.captureSession = [[AVCaptureSession alloc] init]; self.captureSession.sessionPreset = AVCaptureSessionPreset1280x720; if ([self.captureSession canAddInput:self.videoInput]) { [self.captureSession addInput:self.videoInput]; } if ([self.captureSession canAddInput:self.audioInput]) { [self.captureSession addInput:self.audioInput]; } if ([self.captureSession canAddOutput:self.movieFileOutput]) { [self.captureSession addOutput:self.movieFileOutput]; } // 7.预览画面 self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession]; [self.previewView.layer addSublayer:self.previewLayer];}
看到这个方法应该很熟悉了吧,和拍摄照片的初始化话几乎没有区别。需要注意的是第6步sessionPreset设为了AVCaptureSessionPreset1280x720,也就是输出720p的视频。通过查询文档,还有很多不同分辨率可供选择。
还记得开始和结束会话的两个方法吗?�startRunning和stopRunning,真是太好记了:- (void)startSession { if(![self.captureSession isRunning]) { [self.captureSession startRunning]; }}- (void)stopSession { if([self.captureSession isRunning]) { [self.captureSession stopRunning]; }}
最后,在viewDidLoad中调用上文的两个方法:
[self setupCaptureSession]; [self startSession];
真机运行,就可以看到拍摄预览画面了。
完成了初始化工作, 拍摄视频部分,我们只需要调用startRecordingToOutputFileURL:recordingDelegate:方法就可以实现了。其中,参数url是为视频指定的输出路径。
下面我们要为录制按钮建立两个Action,touch down时开始拍摄视频,touch up时结束拍摄。 touch down时,调用startRecord方法:- (void)startRecord { [self.movieFileOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:[self videoPath]] recordingDelegate:self];}
touch up时,调用stopRecord方法:
- (void)stopRecord { if ([self.movieFileOutput isRecording]) { [self.movieFileOutput stopRecording]; }}
我们注意到,开始拍摄时,调用了videoPath方法为视频生成一个输出地址。这个方法使用当前时间戳作为唯一的视频文件名:
- (NSString *)videoPath { NSString *basePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString *moviePath = [basePath stringByAppendingPathComponent: [NSString stringWithFormat:@"%f.mp4",[NSDate date].timeIntervalSince1970]]; return moviePath;}
最后我们,还要实现AVCaptureFileOutputRecordingDelegate代理。拍摄完成后,系统会触发captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error:方法。我们可以通过outputFileURL参数访问拍摄好的视频文件。
拍摄视频和照片的一些高级功能原理是一致的,这里我只将上个Demo中�一块代码拷贝了过来,不用做任何修改就可实现摄像头切换。其他的高级设置可以查阅文档�自行探索。
Demo地址:
转载地址:http://eibnl.baihongyu.com/