使用AVFoundation Framework iPhone进行视频录制?

时间:2010-10-14 11:27:50

标签: iphone ios4 avfoundation

我正在使用WWDC 2010 AVCamDemo示例中的示例代码开发应用程序。在应用程序中,我需要从iPhone的前置摄像头录制视频,但由于我的地方无法使用新的iPhone 4,因此我无法正确测试代码。

如果有人可以告诉我是否正朝着正确的方向前进,我真的很感激。当我设置AVCaptureSession时,我可以在iPhone 3G(升级到iOS 4.1)上测试的有限代码崩溃,如下面的代码所示:

- (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];       
}

1 个答案:

答案 0 :(得分:4)

movieFileOutput在分配后立即释放。 (第9行)