简体   繁体   English

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

[英]Error while recording video on iphone using AVFoundation

Im trying to record video using AVFoundation I can save images but not video. 我试图使用AVFoundation录制视频,但我可以保存图像,但不能保存视频。 When trying to save a video, I got an error saying: 尝试保存视频时,出现错误消息:

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

And here is my code: 这是我的代码:


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

Also I am trying to test this on Iphone 3G with IOs-4.1. 另外,我正在尝试使用IOs-4.1在Iphone 3G上进行测试。

Also worth noting this can happen if you've set the session preset to 'photo'. 同样值得注意的是,如果您将会话预设设置为“照片”,则会发生这种情况。

[session setSessionPreset:kCaptureSessionPresetPhoto];

Which should be: 应该是:

[session setSessionPreset:kCaptureSessionPresetVideo];

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

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

Implement the AVCaptureFileOutputRecordingDelegate protocol. 实现AVCaptureFileOutputRecordingDelegate协议。 Make sure the path of video file is correct and the video file is not exist, as the movie file output does not overwrite existing resources. 确保视频文件的路径正确并且该视频文件不存在,因为电影文件输出不会覆盖现有资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM