Reputation: 1614
I am trying to make a video recording app using AVCaptureSession
.
I have a UIButton which gives the preview layer and sets up the session as follows:
-(void) video:(UIButton*)sender
{
session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetPhoto;
captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
[captureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
captureVideoPreviewLayer.frame = cameraView.bounds;
[cameraView.layer addSublayer:captureVideoPreviewLayer];
NSArray *devices = [AVCaptureDevice devices];
AVCaptureDevice *frontCamera;
AVCaptureDevice *backCamera;
for (AVCaptureDevice *device in devices)
{
if ([device hasMediaType:AVMediaTypeVideo])
{
if ([device position] == AVCaptureDevicePositionBack)
backCamera = device;
else
frontCamera = device;
}
}
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:frontCamera error:nil];
[session addInput:input];
movieOutput = [[AVCaptureMovieFileOutput alloc]init];
if ([session canAddOutput:movieOutput]) {
[session addOutput:movieOutput];
}
[session startRunning];
}
Then one more UIButton to just start recording:
-(void) record:(UIButton*)sender
{
NSLog(@"Recording");
outputURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
outputURL = [outputURL URLByAppendingPathComponent:@"myVideo.mov"];
NSString *filePath = [outputURL path];
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
NSLog(@"is url :: %d", [outputURL isFileURL]);
NSLog(@"does file exists :: %d",[[NSFileManager defaultManager] fileExistsAtPath:filePath]);
[movieOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];
}
Now, I have no idea why this code works on iPad just fine but crashes on iPhone throwing error:
'*** -[AVCaptureMovieFileOutput startRecordingToOutputFileURL:recordingDelegate:] - no active/enabled connections.'
I looked around for solutions for this particular error but if something wrong with my code then why it works on iPad?
Note:
My app is universal.
On iPhones, it loads fine upto preview layer but crashes as soon as I start recording.
I checked on more than one iPhone, same result.
I did implement captureOutput: didFinishRecordingToOutputFileAtURL: fromConnections: error:
UPDATE: After so much of trial and error, I found out that if I add only audio as a input then the app works great on iPhones.
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput * audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];
[session addInput:audioInput];
So problem should be in the way I add camera as input, although I didn't find any. And it works for iPads.
Upvotes: 0
Views: 850
Reputation: 1614
After struggling for 2 days, I found the problem to be sessionPreset
property which was AVCaptureSessionPresetPhoto
. And I solved it by just removing it so that it will use its default value.
Upvotes: 2
Reputation: 3093
try below code may be help you.
-(void) record:(UIButton*)sender
{
NSLog(@"Recording");
[[[self movieOutput] connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:(AVCaptureVideoOrientation)[UIDevice currentDevice].orientation];
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[@"movie" stringByAppendingPathExtension:@"mov"]];
NSLog(@"does file exists :: %d",[[NSFileManager defaultManager] fileExistsAtPath:filePath]);
[[self movieOutput] startRecordingToOutputFileURL:[NSURL fileURLWithPath:filePath] recordingDelegate:self];
}
OR See below link
Upvotes: 0