2010-08-16 59 views
4

使用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」。我所能說的只是告訴我一些我不知道的事情。任何人都知道爲什麼會議沒有運行?

回答

15

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

+1

修復了報告的問題,但現在我得到一個新的錯誤。 Error Domain = NSOSStatusErrorDomain Code = -12673「操作無法完成(OSStatus錯誤-12673。)」UserInfo = 0x154190 {AVErrorRecordingSuccessfullyFinishedKey = false} – iHorse 2010-08-17 16:18:35

+0

movie.mov是否已經存在?另見「stringByAppendingPathComponent:」。 – 2010-08-17 21:37:41

+0

沒有該文件不存在。應該是? – iHorse 2010-08-18 14:59:10