V9Zeros
V9Zeros

Reputation: 206

Recording a video with AVFoundation

I've created the session, the device, input and output but when I call startRecordingToOutputFileURL:recordingDelegate: method, it calls didFinishRecordingToOutputFileAtURL delegate method, so it finishes without starting!

This is my code..

-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

AVCaptureSession *session = [[AVCaptureSession alloc] init];
[session setSessionPreset:AVCaptureSessionPresetPhoto];

AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];


AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];

if ([session canAddInput:deviceInput]) {
[session addInput:deviceInput];
}

AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer *rootLayer = [[self view] layer];
[rootLayer setMasksToBounds:YES];
CGRect frame = self.view.frame;

[previewLayer setFrame:frame];

[rootLayer insertSublayer:previewLayer atIndex:0];

_movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];

if ([session canAddOutput:movieFileOutput]) {
    [session addOutput:movieFileOutput];
}

[session startRunning];
}
-(void)buttonClicked {
[_movieFileOutput startRecordingToOutputFileURL:url recordingDelegate:self];
}

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput
didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
      fromConnections:(NSArray *)connections
            error:(NSError *)error {
    //This method is getting called when I press the button
    NSLog(@"Finished Recording");

}

So what is wrong with my code? Did I miss anything?

Thanks

Upvotes: 0

Views: 242

Answers (1)

V9Zeros
V9Zeros

Reputation: 206

Finally it worked!

The problem was the URL. I've tried many URLs with non of them worked, it has to be in the NSDocumentsDirectory, that's it!

Upvotes: 1

Related Questions