- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
当我开始在 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 中的许多内容时,您只需要 beginConfiguration
和 commitConfiguration
,例如删除输入,更改预设。如果您要实现它,它对前后摄像头切换很有用。在这些调用之间启动或停止 session 并不是什么好事。
关于ios - AVCaptureMovieFileOutput NSInvalidArgumentException 没有事件/启用的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23726879/
我在这里撕扯我的头发。已阅读许多描述如何解决此问题的示例,但没有一个可以帮助我解决该问题。 在 UIView 上有一个链接到 IBAction 的简单按钮。 代码是这样的... 联系方式.h #imp
我在 map 中有一些注解 View ,我希望通过touchUpInside打开一个新的ViewController,但出现此错误: Terminating app due to uncaught
我想尝试一下群众的智慧……因为我现在很沮丧。提前致谢。 这是我的代码: - (void)imagePickerController:(UIImagePickerController *)ipc did
我遇到以下问题。我继承了一个应用程序,我正在尝试修改它。原始功能允许我添加带有一些文本注释的任务。我想添加一个图标以使其更具视觉吸引力。当我修改代码签名以传递图标时(实际上我只是在 NSString
[__NSArrayM relatedObjectDidChange]: unrecognized selector sent to instance error Class: NSInvalidAr
我有两个viewController,都带有一个表,应用程序可以工作,然后我在第二个viewController 中添加了导航栏,让他有机会在第一个viewController 中返回。导航栏可以工作
通常应用程序运行良好,但我在 Crashlytics 中收到问题。一周发生1-2次。适合 2k-3k 用户。应用程序适用于 iOS 7 及更高版本。报告:致命异常:NSInvalidArgumentE
此函数旨在获取 JSON 并根据作为参数发送的对象创建对象数组: + (NSArray *)Object: (id) object FromJSON:(NSData *)objectNotation
尝试将核心数据对象获取到tableview并获得Terminating app due to uncaught exception 'NSInvalidArgumentException', reas
我设计了一个应用程序,它将有一个按钮和三个标签。 当按下按钮时,将调用一个URL。 URL的JSON表示如下: [ { "_id": "50c87e8a1898044ea1458e4c",
我以编程方式实现了自动布局。 self.rightSideLine = [[UIView alloc] init]; self.rightSideLine.backgroundColor = COLO
我有一个 iOS 应用程序,可以在 Appstore 中找到。今天我测试了该应用程序,当应用程序尝试打开错误对话框时,它在我的 iPhone4 (iOS7) 上崩溃了。在 iPhone5s 上没有问题
我在其他地方使用“正常工作”的代码时遇到错误: [Error]: Caught "NSInvalidArgumentException" with reason "*** -[_NSPlacehold
我正在编写代码以向图片添加滤镜。这是代码 func applyFilter(index: Int){ print("Applying Filter....") //let contex
我正在练习创建一个主要不使用 Storyboard的 UI,当我开始调试时,我的应用程序崩溃了,并给我带来了这个错误。 我尝试从 info.plist 和目标中删除“main”,但不起作用 2019-
我有一个 tableView,我在一个单元格上创建了一个 Button。 UIButton *deleteGroupButton = [UIButton buttonWithType:UIButt
这是我的代码: @interface TabRegsViewController () @end @implementation TabRegsViewController @synthesize p
即使在这个项目之前我从未在 Objective-C 中工作过或制作过 iOS 应用程序,但我处于必须支持其他人代码的“令人敬畏”的位置,所以如果我做了一些非常明显或愚蠢的事情,请原谅. 我需要一个带有
我的代码抛出以下异常: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[_
我有一个无法理解的问题,我正在使用一个用于使用Map Open Source Map(https://github.com/route-me/route-me)的库,该示例运行完美,并且我相信不是与代
我是一名优秀的程序员,十分优秀!