iosswiftxcodeavcapturedeviceavcapture

Not able to add input in case of Front camera, Switching camera from back to front?


I am opening my camera in a custom view(cameraView) and It is working fine for Back camera but the issue comes when I switch it to front using a custom turn button(TurnCamera), I got the following:

input "AVCaptureFigVideoDevice: 0x1035c9e80 [Front Camera][com.apple.avfoundation.avcapturedevice.built-in_video:1]"

but my custom camera view is still showing the back camera, and getting captureSession.canAddInput(input) = false, Should I give any access into info.plist for Front camera.

My code for open camera is:-

func camera(){
        let devices = AVCaptureDevice.devices(for: AVMediaType.video)

        currentCaptureDevice = (usingFrontCamera ? getFrontCamera() : getBackCamera())

        print(currentCaptureDevice)

        for device in devices{

            do{
                let input = try AVCaptureDeviceInput(device: currentCaptureDevice!)
                    if captureSession.canAddInput(input){

                        captureSession.addInput(input)
                        sessionOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]

                        if captureSession.canAddOutput(sessionOutput){

                            captureSession.startRunning()
                            captureSession.addOutput(sessionOutput)
                            previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                            previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
                            previewLayer.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
                            cameraView.layer.addSublayer(previewLayer)
                            previewLayer.position = CGPoint(x: self.cameraView.frame.width/2, y: self.cameraView.frame.height/2)
                            cameraView.contentMode = .scaleToFill
                            previewLayer.bounds = cameraView.frame
                        }
                    }
                }
                catch{

                    print("error")
            }
        }
    }

function for get Back camera is:-

func getBackCamera() -> AVCaptureDevice{
        return AVCaptureDevice.default(for: AVMediaType.video)!
    }

function for get front camera is:-

func getFrontCamera() -> AVCaptureDevice?{
        let videoDevices = AVCaptureDevice.devices(for: AVMediaType.video)

        for device in videoDevices{
            let device = device as! AVCaptureDevice
            if device.position == AVCaptureDevice.Position.front {
                return device
            }
        }
        return nil
    }

Button action for switch camera: -

@IBAction func TurnCamera(_ sender: UIButton) {

        usingFrontCamera = !usingFrontCamera
        camera()

    }

Find Demo project on :- https://www.dropbox.com/s/6cfmbh3tnw5y4f7/SwitchingCamera.zip?dl=0

Thanks in Advance


Solution

  • I got the Answer, I was not Removing the capture session inputs, when toggle from back to Front.

    I did changes in Two functions only 1:- Earlier

    func getBackCamera() -> AVCaptureDevice{
            return AVCaptureDevice.default(for: AVMediaType.video)!
        }
    

    Now,

    func getBackCamera() -> AVCaptureDevice{
    
            for i in captureSession.inputs {
                captureSession.removeInput(i as! AVCaptureInput)
            }
    
            return AVCaptureDevice.default(for: AVMediaType.video)!
        }
    

    2:- Earlier,

    func getFrontCamera() -> AVCaptureDevice?{
            let videoDevices = AVCaptureDevice.devices(for: AVMediaType.video)
    
            for device in videoDevices{
                let device = device as! AVCaptureDevice
                if device.position == AVCaptureDevice.Position.front {
                    return device
                }
            }
            return nil
        }
    

    Now,

    func getFrontCamera() -> AVCaptureDevice?{
    
            for i in captureSession.inputs {
                captureSession.removeInput(i as! AVCaptureInput)
            }
    
            let videoDevices = AVCaptureDevice.devices(for: AVMediaType.video)
    
            for device in videoDevices{
                let device = device
                if device.position == AVCaptureDevice.Position.front {
    
                    return device
    
                }
            }
            return nil
        }
    

    Get Demo on :- https://www.dropbox.com/s/p6opina5gtg8nvd/Updated%20Switching%20Camera%202.zip?dl=0