2011-05-12 132 views
1

我試圖使用AVFoundation錄製視頻 我可以保存圖像但不保存視頻。當試圖保存的視頻,我 收到錯誤消息:使用AVFoundation在iPhone上錄製視頻時出現錯誤

[AVCaptureMovieFileOutput startRecordingToOutputFileURL:recordingDelegate:] - no active/enabled connections.' 

這裏是我的代碼:


session = [[AVCaptureSession alloc] init]; 
//session is global object. 
    session.sessionPreset = AVCaptureSessionPresetMedium;    
    AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];  
    captureVideoPreviewLayer.frame = self.imgV.bounds; 
    [self.imgV.layer addSublayer:captureVideoPreviewLayer];   
    AVCaptureDevice *device =[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
    NSError *error = nil; 
    NSLog(@"start  3"); 
    AVCaptureDeviceInput *input = 
    [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; 
    if (!input) { 
     NSLog(@"Error"); 
    } 
    [session addInput:input];  
    stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; 
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil]; 
    [stillImageOutput setOutputSettings:outputSettings];  
    [session addOutput:stillImageOutput];  
    aMovieFileOutput = [[AVCaptureMovieFileOutput alloc] init];  
    [session addOutput:aMovieFileOutput]; 
    [session startRunning]; 
    [self performSelector:@selector(startRecording) withObject:nil afterDelay:10.0]; 
    //[aMovieFileOutput startRecordingToOutputFileURL:fileURL recordingDelegate:self]; 
    //previously i used to do this way but saw people doing it after delay thought it might be taking some time to initialized so tried this way also.   
} 
- (void) startRecording 
{ 
    NSLog(@"startRecording"); 
    NSString *plistPath; 
    NSString *rootPath;  
    rootPath= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    plistPath = [rootPath stringByAppendingPathComponent:@"test.mov"]; 
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:plistPath]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    if ([fileManager fileExistsAtPath:plistPath]) { 
     NSLog(@"file exist %s  n  url %@ ",[rootPath UTF8String],fileURL); 
    } 
    [aMovieFileOutput startRecordingToOutputFileURL:fileURL recordingDelegate:self];  
} 

而且我想與iOS 4.1測試這對iPhone 3G的。

回答

1

您應該在[AVCaptureSession beginConfiguration][AVCaptureSession commitConfiguration]對之間添加輸出。當你傳遞一個對象作爲使用startRecordingToOutputFileURL:recordingDelegate:方法的委託,就可能出現

+0

按照蘋果文檔[AVCaptureSession beginConfiguration]和[AVCaptureSession commitConfiguration]對需要更新當前會話。不過,我開始會話前添加輸出。 – saurabh 2011-05-16 12:19:31

+0

我想知道,如果它的iPhone 3G不支持視頻錄製.. – saurabh 2011-05-16 12:21:15

+0

嘿它的作品...我沒有得到那個例外,但仍然沒有視頻... mov文件只是音頻 – saurabh 2011-05-17 14:00:59

1

這個錯誤,但這個對象沒有 captureOutput:didStartRecordingToOutputFileAtURL:fromConnections: AVCaptureFileOutputRecordingDelegate方法來實現。

0

執行AVCaptureFileOutputRecordingDelegate協議。確保視頻文件的路徑正確並且視頻文件不存在,因爲電影文件輸出不會覆蓋現有資源。

3

另外值得注意的是,如果您已將會話預設置爲「照片」,則會發生這種情況。

[session setSessionPreset:kCaptureSessionPresetPhoto]; 

哪個應該是:

[session setSessionPreset:kCaptureSessionPresetVideo]; 
相關問題