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

发布于 2024-11-07 11:41:26 字数 2471 浏览 3 评论 0原文

我正在尝试使用 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 上测试此代码。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雪花飘飘的天空 2024-11-14 11:41:26

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

[session setSessionPreset:kCaptureSessionPresetPhoto];

应该是:

[session setSessionPreset:kCaptureSessionPresetVideo];

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

[session setSessionPreset:kCaptureSessionPresetPhoto];

Which should be:

[session setSessionPreset:kCaptureSessionPresetVideo];
胡大本事 2024-11-14 11:41:26

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

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

暮年慕年 2024-11-14 11:41:26

当您使用 startRecordingToOutputFileURL:recordingDelegate: 方法将对象作为委托传递,但该对象没有
captureOutput:didStartRecordingToOutputFileAtURL:fromConnections: AVCaptureFileOutputRecordingDelegate 方法已实现。

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.

软糖 2024-11-14 11:41:26

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文