捕获摄像头和音频输入时出错

时间:2010-08-16 17:58:22

标签: iphone video ios4 video-streaming video-capture

使用iOS4中的一些漂亮的新API我试图从iPhone的摄像头和麦克风捕获输入并将其保存到文件中。下面是我正在使用的代码。

AVCaptureSession* captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error];
AVCaptureDeviceInput* videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:captDevice error:&error];
AVCaptureMovieFileOutput * videoOutput = [[AVCaptureMovieFileOutput alloc] init];

if (videoInput && videoOutput && audioInput) 
{
    [captureSession addInput:audioInput];
    [captureSession addInput:videoInput];
    [captureSession addOutput:videoOutput];
    if([captDevice lockForConfiguration:&error])
    {
        if ([captDevice hasTorch]) 
            captDevice.torchMode = AVCaptureTorchModeOn;

        [captDevice unlockForConfiguration];
    }
    else 
    {
        NSLog(@"Could not lock device for config error: %@", error);
    }

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSURL* saveLocationURL = [[NSURL alloc] initFileURLWithPath:[NSString stringWithFormat:@"%@/movie.mov", documentsDirectory]];

    [videoOutput startRecordingToOutputFileURL:saveLocationURL recordingDelegate:self];
    [captureSession startRunning];

    [saveLocationURL release];
}
else 
{
    NSLog(@"Video Error: %@", error);
}   

当didFinishRecordingToOutputFileAtURL返回时,我得到一个神秘的错误响应。

Error Domain=AVFoundationErrorDomain Code=-11803 "Cannot Record" UserInfo=0x152f70 {NSLocalizedRecoverySuggestion=Try recording again., AVErrorRecordingSuccessfullyFinishedKey=false, NSLocalizedDescription=Cannot Record}

错误代码-11803表示“AVErrorSessionNotRunning”。我能说的就是告诉我一些我不知道的事情。任何人都知道为什么会话没有运行?

1 个答案:

答案 0 :(得分:16)

[captureSession startRunning];之前致电[videoOutput startRecordingToOutputFileURL:saveLocationURL recordingDelegate:self];