gpt4 book ai didi

ios - AVCameraInput - 将相机从前切换到后时崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:59:00 26 4
gpt4 key购买 nike

我正在使用 AV 通过我的应用程序录制视频,并且我有一个按钮可以在相机 View (前置和后置相机)之间切换,默认设置为后置。从后面切换到前面工作得很好。但是,从前面切换到后面会导致应用程序崩溃。

- (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 The SWAT App Beta[1769:365194] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* Multiple audio/video AVCaptureInputs are not currently supported.' *** First throw call stack: (0x185c002d4 0x1975c80e4 0x1843ad39c 0x1843accd4 0x10004ac14 0x18a818fb4 0x18a80201c 0x18a818950 0x18a8185dc 0x18a811a74 0x18a7e57f0 0x18aa85274 0x18a7e3d04 0x185bb8250 0x185bb74f4 0x185bb55a4 0x185ae1404 0x18f4eb6fc 0x18a84a2b4 0x10004bb70 0x197c6ea08) libc++abi.dylib: terminating with uncaught exception of type NSException

我不完全确定为什么似乎有多个输入,因为在我列出的代码中我删除了旧的输入,并且从后到前工作得很好。不确定为什么从前到后会让应用程序自行终止。

有什么想法吗?

最佳答案

我通过将用于切换相机的代码重写为我专有的代码来解决我的问题。我创建了一个名为 currentCam 的 NSString,我根据当前情况将文本更改为“Back”和“Front”之间。代码如下:

- (IBAction)btnSwapCamerasClicked:(id)sender {

[session beginConfiguration];


if ([currentCam isEqualToString:@"Back"])
{
NSArray *inputs = [session inputs];

for (AVCaptureInput *input in inputs)
{
[session removeInput:input];
}

//Video input
AVCaptureDevice *newCamera = nil;
newCamera = [self cameraWithPosition:AVCaptureDevicePositionFront];

//Audio input
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput * audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];


NSError *err = nil;
AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:newCamera error:&err];
if(!newVideoInput || err)
{
NSLog(@"Error creating capture device input: %@", err.localizedDescription);
}
else
{

[session addInput:newVideoInput];
[session addInput:audioInput];

newVideoInput = nil;
audioInput = nil;
audioDevice = nil;
newCamera = nil;
inputs = nil;


}

currentCam = @"Front";


}
else if ([currentCam isEqualToString:@"Front"])
{
NSArray *inputs = [session inputs];

for (AVCaptureInput *input in inputs)
{
[session removeInput:input];
}

//Video input
AVCaptureDevice *newCamera = nil;
newCamera = [self cameraWithPosition:AVCaptureDevicePositionBack];

//Audio input
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput * audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];


NSError *err = nil;
AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:newCamera error:&err];
if(!newVideoInput || err)
{
NSLog(@"Error creating capture device input: %@", err.localizedDescription);
}
else
{

[session addInput:newVideoInput];
[session addInput:audioInput];

newVideoInput = nil;
audioInput = nil;
audioDevice = nil;
newCamera = nil;
inputs = nil;
}

currentCam = @"Back";
}
else
{
//Camera is some weird third camera that doesn't exist yet! :O
NSLog(@"wat");
}

[session commitConfiguration];
}

关于ios - AVCameraInput - 将相机从前切换到后时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28837060/

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