lundi 29 décembre 2014

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




Aucun commentaire:

Enregistrer un commentaire