AVFoundation 视频捕获

AVFoundation提供了一系列多媒体捕捉的方式,核心类为如下几个:AVCaptureSession、AVCaptureInput、AVCaptureOutput。AVCaptureSession负责协调媒体输入和输出。

AVCaptureSession

配置AVCaptureSession

//Config AVCaptureSession AVCaptureSession *session = [[AVCaptureSession alloc] init];[session beginConfiguration]; if ([session canSetSessionPreset:AVCaptureSessionPreset1280x720]) { [session setSessionPreset:AVCaptureSessionPreset1280x720]; } [session commitConfiguration]; self.captureSession = session;

AVCaptureInput

有了Session之后,就需要配置输入,输出了。媒体捕捉的输入主要来自于设备,使用AVCaptureInput一个子类——AVCaptureDeviceInput。

配置AVCaptureDeviceInput

NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; for (AVCaptureDevice *device in devices) { if (device.position == AVCaptureDevicePositionBack) {//这里使用后置摄像头 self.backCamera = device; self.backCameraInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:nil]; } } if ([self.captureSession canAddInput:self.backCameraInput]) { [self.captureSession addInput:self.backCameraInput]; }

AVCaptureOutput

常用的输出有AVCaptureVideoDataOutput、AVCaptureAudioDataOutput、AVCaptureStillImageOutput、AVCaptureFileOutput。

AVCaptureStillImageOutput

AVCaptureStillImageOutput *stillOutput = [[AVCaptureStillImageOutput alloc] init]; self.imageOutput = stillOutput; NSDictionary *imageSetting = @{AVVideoCodecKey: AVVideoCodecJPEG}; [self.imageOutput setOutputSettings:imageSetting]; if ([self.captureSession canAddOutput:audioDataOutput]) { [self.captureSession addOutput:audioDataOutput]; }

AVCaptureVideoPreviewLayer

配置好输入输出,我们还需要看到已经捕捉到的图像的样子,这里使用AVCaptureVideoPreviewLayer。
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession]; self.previewLayer.frame = [UIScreen mainScreen].bounds; [self.view.layer addSublayer:self.previewLayer];
配置好一起之后,可以开始捕获了。
[self.captureSession startRunning];

获取图片

使用图片输出获取一张静态图片
-(void)captureImage { AVCaptureConnection *imageConnection = nil; for (AVCaptureConnection *connect in self.imageOutput.connections) { for (AVCaptureInputPort *port in connect.inputPorts) { if (port.mediaType == AVMediaTypeVideo) { imageConnection = connect; break; } } if (imageConnection != nil) { break; } } if (imageConnection != nil) { [self.imageOutput captureStillImageAsynchronouslyFromConnection:imageConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { // UIImage *image = [[UIImage alloc] initWithData:[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]]; // NSLog(@"get image success: %@", image); CVImageBufferRef imageRef = CMSampleBufferGetImageBuffer(imageDataSampleBuffer); }]; } }

录音

AVCapture捕获音频、播放音频有专门的一个类,如下代码。
[UIDevice currentDevice].proximityMonitoringEnabled = YES; if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending) { AVAudioSession *session = [AVAudioSession sharedInstance]; NSError *sessionError; //如果此时手机靠近面部放在耳朵旁,那么声音将通过听筒输出,并将屏幕变暗 if ([[UIDevice currentDevice] proximityState] == YES) { NSLog(@"Device is close to user"); [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; } else { NSLog(@"Device is not close to user"); [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; } if (session == nil) { NSLog(@"Error creating session: %@", sessionError); } else { [session setActive:YES error:nil]; } } NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *playName = [NSString stringWithFormat:@"%@/play.acc", docDir]; if ([[NSFileManager defaultManager] fileExistsAtPath:playName]) { [[NSFileManager defaultManager] removeItemAtPath:playName error:nil]; } NSDictionary *recorderSettingsDict = @{ AVFormatIDKey:@(kAudioFormatMPEG4AAC), AVSampleRateKey: @(44100), AVNumberOfChannelsKey:@(2), AVLinearPCMBitDepthKey:@(8), AVLinearPCMIsBigEndianKey:@(NO), AVLinearPCMIsFloatKey:@(NO) }; NSError *error = nil; self.recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:playName] settings:recorderSettingsDict error:&error]; if (self.recorder) { self.recorder.meteringEnabled = YES; [self.recorder prepareToRecord]; [self.recorder record]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self.recorder stop]; //如果此时手机靠近面部放在耳朵旁,那么声音将通过听筒输出,并将屏幕变暗 if ([[UIDevice currentDevice] proximityState] == YES) { NSLog(@"Device is close to user"); [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; } else { NSLog(@"Device is not close to user"); [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; } self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:playName] error:nil]; // [self.audioPlayer prepareToPlay]; [self.audioPlayer play]; }); }

你可能感兴趣的:(AVFoundation 视频捕获)