Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios AVCameraInput-将摄像头从前向后切换时崩溃_Ios_Xcode_Avfoundation_Video Capture - Fatal编程技术网

Ios AVCameraInput-将摄像头从前向后切换时崩溃

Ios AVCameraInput-将摄像头从前向后切换时崩溃,ios,xcode,avfoundation,video-capture,Ios,Xcode,Avfoundation,Video Capture,我使用AV通过我的应用程序录制视频,我有一个按钮,可以在前后摄像头视图之间切换,默认为back。从后面切换到前面很好。然而,从前面切换到后面会导致应用程序崩溃 - (IBAction)btnSwapCamerasClicked:(id)sender { //Change camera source if(session) { //Indicate that some changes will be made to the session [session beginConfig

我使用AV通过我的应用程序录制视频,我有一个按钮,可以在前后摄像头视图之间切换,默认为back。从后面切换到前面很好。然而,从前面切换到后面会导致应用程序崩溃

- (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应用程序测试版[1769:365194]*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“*当前不支持多个音频/视频AVCAPTURE输入。” ***第一次抛出调用堆栈: (0x185c002d4 0x1975c80e4 0x1843ad39c 0x1843ACD4 0x10004ac14 0x18a818fb4 0x18a80201c 0x18a818950 0x18a8185dc 0x18a811a74 0x18a7e57f0 0x18aa85274 0x18a7e3d04 0x185bb8250 0x185bb74f4 0x185bb55a4 0x185ae1404 0x18f4eb6fc 0x18a84a2b4 0x10004B70 0x197c6ea08) libc++abi.dylib:以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];
}

只有这么多代码才能工作

- (IBAction)switchCamera:(id)sender {

    [captureSession beginConfiguration];
    NSArray *inputs = [captureSession inputs];

    //Remove all inputs
    for (AVCaptureInput *input in inputs)
    {
        [captureSession removeInput:input];
    }

    //Video input
    AVCaptureDevice *newCamera = nil;
    if ([currentCam isEqualToString:@"Back"]){
        newCamera = [self cameraWithPosition:AVCaptureDevicePositionFront];
           currentCam = @"Front";
    }else{
        newCamera = [self cameraWithPosition:AVCaptureDevicePositionBack];
         currentCam = @"Back";
    }
    //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
    {
        [captureSession addInput:newVideoInput];
        [captureSession addInput:audioInput];
    }
[captureSession commitConfiguration];
}

更新--我想我找到了可能导致问题的原因。当我再次尝试前后移动时,检查相机位置的if语句不起作用。我重写了if,将“else if cam is front”添加到else条件和第三个else中,然后我的代码传递到第三个else,它不检查任何条件。更新2:这让我找到了答案。我使用的解决方案见下文。