使用AVFoundation在iphone上录制视频时出错

时间:2011-05-12 14:56:34

标签: iphone video-capture avfoundation

我尝试使用AVFoundation录制视频 我可以保存图像而不是视频。在尝试保存视频时,我 得到一个错误说:

[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上进行测试。

4 个答案:

答案 0 :(得分:4)

另外值得注意的是,如果您将会话预设设置为“照片”,则会发生这种情况。

[session setSessionPreset:kCaptureSessionPresetPhoto];

应该是:

[session setSessionPreset:kCaptureSessionPresetVideo];

答案 1 :(得分:1)

您应该在[AVCaptureSession beginConfiguration][AVCaptureSession commitConfiguration]对之间添加输出。

答案 2 :(得分:1)

使用startRecordingToOutputFileURL:recordingDelegate:方法将对象作为委托传递时可能会发生此错误,但此对象没有 captureOutput:didStartRecordingToOutputFileAtURL:fromConnections:实现AVCaptureFileOutputRecordingDelegate方法。

答案 3 :(得分:0)

实施AVCaptureFileOutputRecordingDelegate协议。确保视频文件的路径正确且视频文件不存在,因为电影文件输出不会覆盖现有资源。