【发布时间】:2015-05-04 09:56:54
【问题描述】:
我正在使用 AV 通过我的应用程序录制视频,并且我有一个按钮,可以在前后摄像头的摄像头视图之间切换,默认为后背。从后到前切换效果很好。但是,从前向后切换会导致应用崩溃。
- (IBAction)btnSwapCamerasClicked:(id)sender {
//Change camera source
if(session)
{
//Indicate that some changes will be made to the session
[session beginConfiguration];
//Remove existing input
AVCaptureInput* currentCameraInput = [session.inputs objectAtIndex:0];
[session removeInput:currentCameraInput];
//Get new input
AVCaptureDevice *newCamera = nil;
if(((AVCaptureDeviceInput*)currentCameraInput).device.position == AVCaptureDevicePositionBack)
{
newCamera = [self cameraWithPosition:AVCaptureDevicePositionFront];
}
else
{
newCamera = [self cameraWithPosition:AVCaptureDevicePositionBack];
}
//Add input to session
NSError *err = nil;
AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:newCamera error:&err];
if(!newVideoInput || err)
{
NSLog(@"Error creating capture device input: %@", err.localizedDescription);
}
else
{
//THIS IS THE SPOT THAT CRASHES.
[session addInput:newVideoInput];
}
//Commit all the configuration changes at once
[session commitConfiguration];
}
}
崩溃发生在 [session addInput:newVideoInput];我返回以下错误文本:
2015-03-03 11:25:59.566 SWAT App Beta[1769:365194] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* 多个音频/视频 AVCaptureInputs 是目前不支持。 *** 首先抛出调用堆栈: (0x185c002d4 0x1975c80e4 0x1843ad39c 0x1843accd4 0x10004ac14 0x18a818fb4 0x18a80201c 0x18a818950 0x18a8185dc 0x18a811a74 0x18a7e57f0 0x18aa85274 0x18a7e3d04 0x185bb8250 0x185bb74f4 0x185bb55a4 0x185ae1404 0x18f4eb6fc 0x18a84a2b4 0x10004bb70 0x197c6ea08) libc++abi.dylib:以 NSException 类型的未捕获异常终止
我不完全确定为什么似乎有多个输入,因为在我列出的代码中,我删除了旧输入,而且它从后到前工作得很好。不知道为什么从前到后会让应用程序自毁。
有什么想法吗?
【问题讨论】:
-
更新——我想我发现了一些可能导致问题的东西。当我试图从前到后再次检查相机位置时,我的 if 语句不起作用。我重写了 if 以将“else if cam is front”添加到 else 条件和第三个 else,我的代码传递给第三个 else,它不检查任何条件。
-
更新 2:这让我得到了答案。我使用的解决方案见下文。
标签: ios xcode avfoundation video-capture