一、音频采集
1.创建会话层
captureSession = [[AVCaptureSession alloc]init];
2.初始化音频采集设备
audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
3.音频输入
NSError *error;
audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error];
4.将音频输入添加到会话层
if([captureSession canAddInput:audioInput]){
[captureSession addInput:audioInput];
}
5.音频输出
audioOutput = [[AVCaptureAudioDataOutput alloc]init];
6.音频输出代理
[audioOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
7.将音频输出添加到会话层
if([captureSession canAddOutput:audioOutput]){
[captureSession addOutput:audioOutput];
}
8.输出连接到媒体层
[audioOutput connectionWithMediaType:AVMediaTypeAudio];
9.开始采集
[captureSession startRunning];
二、视频采集
1.设置分辨率
captureSession.sessionPreset = AVCaptureSessionPresetMedium;
2.初始化视频采集设备
videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
3.视频输入
videoInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
4.将视频输入添加到会话层
if([captureSession canAddInput:videoInput]){
[captureSession addInput:videoInput];
}
5.视频输出
videoOutput =[[AVCaptureVideoDataOutput alloc]init];
6.将视频输出添加到会话层
if([captureSession canAddOutput:videoOutput]){
[captureSession addOutput:videoOutput];
}
7.视频数据处理队列
videoDataQueue = dispatch_queue_create("com.ksyun.queue",NULL);
8.视频输出代理
[videoOutput setSampleBufferDelegate:self queue:videoDataQueue];
9.预览layer
previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
previewLayer.frame = self.view.frame;
[self.view.layer addSubLayer:previewLayer];
10.设置帧率
videoOutput.minFrameDuration = CMTime(1, 15);
11.开始采集
[captureSession startRunning];