views:

236

answers:

1

i am developing an application with the help of sample code from the WWDC 2010 http://developer.apple.com/videos/wwdc/2010/ example - AVCamDemo. in which i need to record a video from front camera of iPhone...since new iPhone 4 is not available at my place i am not able to test the code properly.. i would be really thankful if someone can give me a heads up weather i m going in the right direction or not...the limited code i could test on my iPhone 3G(upgraded to iOS 4.1) crashes when i set the AVCaptureSession as shown in the code thnku

-(void)recordVideo
{

NSLog(@"video recording on");

AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

AVCaptureDeviceInput *videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:videoCaptureDevice error:nil];  

AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];

[movieFileOutput release];

AVCaptureSession *session = [[AVCaptureSession alloc] init];

[session addInput:videoInput];

[session addOutput:movieFileOutput];

[self setSession:session]; // crashes


if (![session isRunning])
{
    [self performSelector:@selector(startRecording) withObject:nil afterDelay:1.0];
    [session startRunning];

}

- (void) startRecording
{

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
{

    NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];
    NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:outputPath]) {
        NSLog(@"file saved");
    }
    [outputPath release];
    return [outputURL autorelease];
}

+ (AVCaptureConnection *)connectionWithMediaType:(NSString *)mediaType fromConnections:(NSArray *)connections;
{
    for ( AVCaptureConnection *connection in connections ) {
        for ( AVCaptureInputPort *port in [connection inputPorts] ) {
            if ( [[port mediaType] isEqual:mediaType] ) {
                return [[connection retain] autorelease];
            }
        }
    }
    return nil;
}

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput
didStartRecordingToOutputFileAtURL:(NSURL *)fileURL
                   fromConnections:(NSArray *)connections
{

}

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput
didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
                    fromConnections:(NSArray *)connections
                              error:(NSError *)error
{
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:outputFileURL]) {
        [library writeVideoAtPathToSavedPhotosAlbum:outputFileURL
                                    completionBlock:^(NSURL *assetURL, NSError *error)];
    }

    [library release];    

  }
A: 

I am also doing same thing...code will run good on my iphone 3gs but at the time of saving video it will giving me msg that asset library not supported and it save that recorded audion only..if you have any idea of how to save it i local cache plz do tell it to me.

Gaurav aka sparsh