I am trying to record video from the front camera of an iPhone 4 using the AVFoundation Framework using the WWDC samples I received from the iPhone developer program. But I still can't get it to work. The video is not recorded or not saved in my iPhone library ... here is the code I'm trying to use ... would it be very useful if someone helped me with the problem I have?
-(void)recordVideo { AVCaptureDeviceInput *videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self backFacingCamera] error:nil]; AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init]; AVCaptureSession *session = [[AVCaptureSession alloc] init]; [session addInput:videoInput]; [session addOutput:movieFileOutput]; [movieFileOutput release]; if (![session isRunning]) { [self performSelector:@selector(startRecording) withObject:nil afterDelay:1.0]; [session startRunning]; } } - (void) startRecording { NSLog(@"start recording"); AVCaptureConnection *videoConnection = [playVideo connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self movieFileOutput] connections]]; if ([videoConnection isVideoOrientationSupported]) { [videoConnection setVideoOrientation:[self orientation]]; } [[self movieFileOutput] startRecordingToOutputFileURL:[self tempFileURL] recordingDelegate:self]; } - (void) stopRecording { NSLog(@"stop recording"); [[self movieFileOutput] stopRecording]; } - (NSURL *) tempFileURL { NSLog(@"temp file url"); NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"]; NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath]; NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:outputPath]) { NSLog(@"exists"); } [outputPath release]; return [outputURL autorelease]; } - (void) setConnectionWithMediaType:(NSString *)mediaType enabled:(BOOL)enabled; { [[playVideo connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self movieFileOutput] connections]] setEnabled:enabled]; } + (AVCaptureConnection *)connectionWithMediaType:(NSString *)mediaType fromConnections:(NSArray *)connections; { NSLog(@"connection with media type"); for ( AVCaptureConnection *connection in connections ) { for ( AVCaptureInputPort *port in [connection inputPorts] ) { if ( [[port mediaType] isEqual:mediaType] ) { return [[connection retain] autorelease]; } } } return nil; } @implementation recordVideo (Internal) - (AVCaptureDevice *) cameraWithPosition:(AVCaptureDevicePosition) position { NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; for (AVCaptureDevice *device in devices) { if ([device position] == position) { return device; } } return nil; } - (AVCaptureDevice *) backFacingCamera { NSLog(@"back"); return [self cameraWithPosition:AVCaptureDevicePositionBack]; } - (AVCaptureDevice *) frontFacingCamera { NSLog(@"front "); return [self cameraWithPosition:AVCaptureDevicePositionFront]; } @end @implementation recordVideo (AVCaptureFileOutputRecordingDelegate) - (void) captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL *)fileURL fromConnections:(NSArray *)connections { NSLog(@"did start recording"); } - (void) captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error { NSLog(@"did finish recording output file"); ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:outputFileURL]) { [library writeVideoAtPathToSavedPhotosAlbum:outputFileURL completionBlock:^(NSURL *assetURL, NSError *error){ if (error && [delegate respondsToSelector:@selector(assetLibraryError:forURL:)]) { [delegate assetLibraryError:error forURL:assetURL]; } }]; } else { } [library release]; } @end
source share