gpt4 book ai didi

ios - AVCaptureMovieFileOutput NSInvalidArgumentException 没有事件/启用的连接

转载 作者:可可西里 更新时间:2023-11-01 03:31:13 26 4
gpt4 key购买 nike

当我开始在 viewController 中录制视频时,我偶尔会遇到 NSInvalidArgumentException 异常,但只是在之前的 View Controller 中拍摄照片之后。我尝试了 Google 和 So 的一些建议,但在调用 startRecordingToOutputFileURL:fileURL 时仍然遇到此错误。

如果我不访问拍摄照片的其他 View Controller ,我永远不会收到错误 - 它只会在我拍照时发生,然后切换到进行视频录制的新 View Controller 。

我认为拍照留下了一些痕迹,但是当我初始化我的录像机 View Controller 时,我在设置 session 和诸如此类的东西时没有遇到任何错误。任何想法发生了什么或如何从中恢复?为什么是 NSInvalidArgumentException 异常?谢谢!

这是我的代码:

dispatch_async(dispatch_get_main_queue(), ^{

// Try to Fix bug:
// http://stackoverflow.com/questions/5979962/error-while-recording-video-on-iphone-using-avfoundation

[self.captureSession beginConfiguration];

// Ensure session is running
if ( [self.captureSession isRunning] == NO ) {
NSLog(@"Capture session is NOT running... Starting it now!");
[self.captureSession startRunning];
}
else {
NSLog(@"Capture session is ALREADY running...");
}

NSLog(@"File URL is: %@",fileURL);
NSLog(@"FileOutput is: %@",self.fileOutput);

[self.fileOutput startRecordingToOutputFileURL:fileURL recordingDelegate:self];

// Try to Fix bug:
// http://stackoverflow.com/questions/5979962/error-while-recording-video-on-iphone-using-avfoundation
[self.captureSession commitConfiguration];

});

这是错误回溯:

2014-05-18 16:01:38.818 app[1699:60b] *** Start recording
2014-05-18 16:01:38.820 app[1699:60b] Capture session is ALREADY running...
2014-05-18 16:01:38.827 app[1699:60b] Capture session is ALREADY running...
2014-05-18 16:01:38.828 app[1699:60b] File URL is: file:////var/mobile/Applications/73FFC590-05A8-4D74-82D9-EBA122B00A20/Documents/2014-05-18-16-01-38-0.mp4
2014-05-18 16:01:38.828 app[1699:60b] FileOutput is: <AVCaptureMovieFileOutput: 0x16513b10>
2014-05-18 16:01:38.829 app[1699:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVCaptureMovieFileOutput startRecordingToOutputFileURL:recordingDelegate:] - no active/enabled connections.'
*** First throw call stack:
(0x2fe5ff0b 0x3a5f6ce7 0x2ed5751d 0xfb4b5 0x3aadfd53 0x3aadfd3f 0x3aae26c3 0x2fe2a681 0x2fe28f4d 0x2fd93769 0x2fd9354b 0x34d006d3 0x326f2891 0xe40c9 0x3aaf4ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

这是 captureSession 的初始化方式(来自此处的 OpenSource 项目:https://github.com/shu223/SlowMotionVideoRecorder):

- (id)initWithPreviewView:(UIView *)previewView {

self = [super init];

if (self) {

NSError *error;

self.captureSession = [[AVCaptureSession alloc] init];
self.captureSession.sessionPreset = AVCaptureSessionPresetInputPriority;

AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];

if (error) {
NSLog(@"Video input creation failed");
return nil;
}

if (![self.captureSession canAddInput:videoIn]) {
NSLog(@"Video input add-to-session failed");
return nil;
}
[self.captureSession addInput:videoIn];


// save the default format
self.defaultFormat = videoDevice.activeFormat;
defaultVideoMaxFrameDuration = videoDevice.activeVideoMaxFrameDuration;


AVCaptureDevice *audioDevice= [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput *audioIn = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error];
[self.captureSession addInput:audioIn];

self.fileOutput = [[AVCaptureMovieFileOutput alloc] init];
[self.captureSession addOutput:self.fileOutput];


self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
self.previewLayer.frame = previewView.bounds;
self.previewLayer.contentsGravity = kCAGravityResizeAspectFill;
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[previewView.layer insertSublayer:self.previewLayer atIndex:0];

[self.captureSession startRunning];
}
return self;
}

我的代码在 viewDidLoad 中使用了这样的初始化代码:

self.captureManager = [[AVCaptureManager alloc] initWithPreviewView:self.view];
self.captureManager.delegate = self;

实际开始和停止录制的代码是通过 IBAction 方法完成的,如下所示:

- (IBAction)recButtonTapped:(id)sender {

// REC START
if (self.captureManager.isRecording == NO ) {

NSLog(@"*** Start recording");

// change UI
[self.recBtn setImage:self.recStopImage
forState:UIControlStateNormal];
self.fpsControl.enabled = NO;

// timer start
startTime = [[NSDate date] timeIntervalSince1970];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01
target:self
selector:@selector(timerHandler:)
userInfo:nil
repeats:YES];

[self.captureManager startRecording];

}
// REC STOP
else {

NSLog(@"*** Stop recording");

isNeededToSave = YES;
[self.captureManager stopRecording];

[self.timer invalidate];
self.timer = nil;

// change UI
[self.recBtn setImage:self.recStartImage
forState:UIControlStateNormal];
self.fpsControl.enabled = YES;

}
}

编辑 - 我肯定会在照片 View 中关闭 session ,这是该代码。当我离开照片 View Controller 时,我确认它正在被调用。

        NSLog(@"RELEASE PHOTO SESSION NOW!");

for(AVCaptureInput *input1 in _mySesh.inputs) {
[_mySesh removeInput:input1];
}

for(AVCaptureOutput *output1 in _mySesh.outputs) {
[_mySesh removeOutput:output1];
}


[_mySesh stopRunning];

// Fix closing of session
dispatch_after(
dispatch_time(0,500000000),
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
_mySesh = nil;
}

);
更新 #####

根据下面唯一的答案,我试图在开始录制之前“取消链接”文件。还是不行。

NSURL *fileURL = [NSURL URLWithString:[@"file://" stringByAppendingString:filePath]];

//NSLog(@"Beginning to record to output file...");

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

// Wait for session to start
//[NSThread sleepForTimeInterval:1.0];

dispatch_async(dispatch_get_main_queue(), ^{

// Ensure session is running
if ( [self.captureSession isRunning] == NO ) {
NSLog(@"Capture session is NOT running... Starting it now!");
[self.captureSession startRunning];
}
else {
NSLog(@"Capture session is ALREADY running...");
}

NSLog(@"File URL is: %@",fileURL);
NSLog(@"FileOutput is: %@",self.fileOutput);

// Delete the file
unlink([[@"file://" stringByAppendingString:filePath] UTF8String]);

[self.fileOutput startRecordingToOutputFileURL:fileURL recordingDelegate:self];

});

});
更新

为了后代,我正在调用“didFinishRecordingToOutputFileAtURL”委托(delegate)方法:

- (void)                 captureOutput:(AVCaptureFileOutput *)captureOutput
didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
fromConnections:(NSArray *)connections error:(NSError *)error
{

// Print any errors
if ( error ) {
NSLog(@"Error Recording Video! %@",error.localizedDescription);
}

_isRecording = NO;

if ([self.delegate respondsToSelector:@selector(didFinishRecordingToOutputFileAtURL:error:)]) {
[self.delegate didFinishRecordingToOutputFileAtURL:outputFileURL error:error];
}

}

最佳答案

首先,先启动AVCaptureSession。之后,您可以创建 AVPreviewLayer 和 AVCaptureMovieFileOutput。

其次,在向捕获 session 添加任何内容之前使用-[AVCaptureSession canAddOutput:]-[AVCaptureSession canAddInput:],这将为您节省大量时间和挫败感。

第三,当您想一次更改 captureSession 中的许多内容时,您只需要 beginConfigurationcommitConfiguration,例如删除输入,更改预设。如果您要实现它,它对前后摄像头切换很有用。在这些调用之间启动或停止 session 并不是什么好事。

关于ios - AVCaptureMovieFileOutput NSInvalidArgumentException 没有事件/启用的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23726879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com