I am developing an application for the iPhone. In this case, pause and resume the camera. So I used AVFoundation to do this instead of using UIImagePickerController .
My code is:
- (void) startup :(BOOL)isFrontCamera { if (_session == nil) { NSLog(@"Starting up server"); self.isCapturing = NO; self.isPaused = NO; _currentFile = 0; _discont = NO; // create capture device with video input _session = [[AVCaptureSession alloc] init]; AVCaptureDevice *cameraDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if(isFrontCamera) { NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; AVCaptureDevice *captureDevice = nil; for (AVCaptureDevice *device in videoDevices) { if (device.position == AVCaptureDevicePositionFront) { captureDevice = device; break; } } cameraDevice = captureDevice; } cameraDevice=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:cameraDevice error:nil]; [_session addInput:input]; // audio input from default mic AVCaptureDevice* mic = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio]; AVCaptureDeviceInput* micinput = [AVCaptureDeviceInput deviceInputWithDevice:mic error:nil]; [_session addInput:micinput]; // create an output for YUV output with self as delegate _captureQueue = dispatch_queue_create("uk.co.gdcl.cameraengine.capture", DISPATCH_QUEUE_SERIAL); AVCaptureVideoDataOutput* videoout = [[AVCaptureVideoDataOutput alloc] init]; [videoout setSampleBufferDelegate:self queue:_captureQueue]; NSDictionary* setcapSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange], kCVPixelBufferPixelFormatTypeKey, nil]; videoout.videoSettings = setcapSettings; [_session addOutput:videoout]; _videoConnection = [videoout connectionWithMediaType:AVMediaTypeVideo]; [_videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait]; NSDictionary* actual = videoout.videoSettings; _cy = [[actual objectForKey:@"Width"] integerValue]; _cx = [[actual objectForKey:@"Height"] integerValue]; AVCaptureAudioDataOutput* audioout = [[AVCaptureAudioDataOutput alloc] init]; [audioout setSampleBufferDelegate:self queue:_captureQueue]; [_session addOutput:audioout]; _audioConnection = [audioout connectionWithMediaType:AVMediaTypeAudio]; [_session startRunning]; _preview = [AVCaptureVideoPreviewLayer layerWithSession:_session]; _preview.videoGravity = AVLayerVideoGravityResizeAspectFill; } }
Here I encountered a problem when I change the camera to Front. when I call the above method, changing the camera to the foreground, the preview layer is stuck and the preview does not appear. I doubt: "Can we change the capture in the middle of the capture session?". Let me know where I am wrong (or). Suggest me a solution on how to move between the front and rear cameras during recording.
Thanks at Advance.
source share