Error while recording video on iphone using AVFoundation

∥☆過路亽.° 提交于 2019-12-11 08:34:40

问题


Im trying to record video using AVFoundation I can save images but not video. 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.


回答1:


You should be adding your outputs between a [AVCaptureSession beginConfiguration] and [AVCaptureSession commitConfiguration] pair.




回答2:


Also worth noting this can happen if you've set the session preset to 'photo'.

[session setSessionPreset:kCaptureSessionPresetPhoto];

Which should be:

[session setSessionPreset:kCaptureSessionPresetVideo];



回答3:


This error can occur when you pass an object as a delegate using startRecordingToOutputFileURL:recordingDelegate: method, but this object does not have the captureOutput:didStartRecordingToOutputFileAtURL:fromConnections: AVCaptureFileOutputRecordingDelegate method implemented.




回答4:


Implement the AVCaptureFileOutputRecordingDelegate protocol. 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.



来源:https://stackoverflow.com/questions/5979962/error-while-recording-video-on-iphone-using-avfoundation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!