I made an app with a feature to capture and save video. I have used AVFoundation for that and Apple's AVCam has been my guide.
I hope I can make it clear:
Everything works fine until I release the videoViewController that handles the AVCamCaptureManager for the first time (In AVCam that would be AVCamViewController). After that when I create it again, the video freezes right after camera switch. Even re-run will not help, nor will clean, or device reset. (Sometimes one of the things help, but it is not a rule).
I release the videoViewController when not needed to save memory.
Code for switching the camera is basically the same as in AVCam:
NSError *error;
AVCaptureDeviceInput *newVideoInput;
AVCaptureDevicePosition position = currentVideoInput.device.position;
if (position == AVCaptureDevicePositionBack)
newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:frontFacingCamera error:&error];
else if (position == AVCaptureDevicePositionFront)
newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:backFacingCamera error:&error];
if (newVideoInput != nil) {
[session beginConfiguration];
[session removeInput:currentVideoInput];
if ([session canAddInput:newVideoInput]) {
[session addInput:newVideoInput];
[self setVideoInput:newVideoInput];
} else {
[session addInput:currentVideoInput];
}
[session commitConfiguration];
[newVideoInput release];
} else if (error) {
NSLog(@"%@",[error localizedDescription]);
}
Code with which I dismiss videoView
[self.videoViewController.view removeFromSuperview];
self.videoViewController = nil;
My current "workaround" is to simply leave it be, even if I do not need it.
Can someone explain why is this happening and how to solve it.
EDIT: Solved
As W Dyson pointed out in his response I should have stopped the session before releasing my videoViewController like so:
[[[self.videoViewController captureManager] session] stopRunning];
[self.videoViewController.view removeFromSuperview];
self.videoViewController = nil;
Thanks W Dyson.