Can't get any video preview with AVCaptureSession
-
13-02-2021 - |
Question
I set up my AVCaptureSession
like so:
AVCaptureSession *newSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *newInput = [[AVCaptureDeviceInput alloc] initWithDevice:camera error:&error];
if(error) {
NSLog([error localizedDescription]);
}
AVCaptureMovieFileOutput *newOutput = [[AVCaptureMovieFileOutput alloc] init];
if([newSession canAddInput:newInput])
[newSession addInput:newInput];
if ([newSession canAddOutput:newOutput])
[newSession addOutput:newOutput];
[self setSession:newSession];
[self setInput:newInput];
[self setOutput:newOutput];
And then I added a preview layer to my view:
AVCaptureVideoPreviewLayer *layer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[recorder session]];
[layer setBounds:[recordingView bounds]];
[layer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[[recordingView layer] insertSublayer:layer above:[recordingView layer]];
But I wont get any preview.
I'm on IOS 5 with an iPod Touch 4th Gen.
Thanks.
Solution
First of all, how is your recordingView
defined?
Second,
[[recordingView layer] insertSublayer:layer above:[recordingView layer]];
makes no sense. (you are trying to add a layer above the own layer inside the own layer ->duh?)
Try [recordingView.layer addSublayer:layer];
instead.
Your posted project in the comments uses zeroing weak pointers. mark your properties strong instead and everything works.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow