Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift AVCaptureSession Close Open Button Error : Multiple audio/video AVCaptureInputs are not currently supported

I have a working barcode scanner code. When I click the openCamera button, first time everything is good. When I click the closeCamera button, good, but if I click again the openCamera button gives a fatal error. Code and error are below. In fact, is it possible to toggle camera view with one button?

// Barcode Camera Properties
let captureSession = AVCaptureSession()
var captureDevice:AVCaptureDevice?
var captureLayer:AVCaptureVideoPreviewLayer?

override func viewDidLoad() {
    super.viewDidLoad()
    self.cameraView.alpha = 0
}

@IBAction func closeCamera(sender: AnyObject) {
    self.captureLayer!.hidden = true
    self.captureSession.stopRunning()
}

@IBAction func openCamera(sender: AnyObject) {
    self.cameraView.alpha = 1
    self.cameraView.animate()
    setupCaptureSession()
}

//MARK: Session Startup
private func setupCaptureSession(){
    self.captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    do {
        let deviceInput = try AVCaptureDeviceInput(device: captureDevice) as AVCaptureDeviceInput
        //Add the input feed to the session and start it
        self.captureSession.addInput(deviceInput)
        self.setupPreviewLayer({
            self.captureSession.startRunning()
            self.addMetaDataCaptureOutToSession()
        })
    } catch let setupError as NSError {
        self.showError(setupError.localizedDescription)
    }
}

private func setupPreviewLayer(completion:() -> ()){
    self.captureLayer = AVCaptureVideoPreviewLayer(session: captureSession) as AVCaptureVideoPreviewLayer
    if let capLayer = self.captureLayer {
        capLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
        capLayer.frame = self.cameraView.frame
        self.view.layer.addSublayer(capLayer)
        completion()
    } else {
        self.showError("An error occured beginning video capture.")
    }
}

//MARK: Metadata capture
func addMetaDataCaptureOutToSession() {
    let metadata = AVCaptureMetadataOutput()
    self.captureSession.addOutput(metadata)
    metadata.metadataObjectTypes = metadata.availableMetadataObjectTypes
    metadata.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
}

//MARK: Delegate Methods
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
    for metaData in metadataObjects {
        let decodedData:AVMetadataMachineReadableCodeObject = metaData as! AVMetadataMachineReadableCodeObject
        self.pCodeTextField.text = decodedData.stringValue
        //decodedData.type
    }
}

//MARK: Utility Functions
func showError(error:String) {
    let alertController = UIAlertController(title: "Error", message: error, preferredStyle: .Alert)
    let dismiss:UIAlertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Destructive, handler:{(alert:UIAlertAction) in
        alertController.dismissViewControllerAnimated(true, completion: nil)
    })
    alertController.addAction(dismiss)
    self.presentViewController(alertController, animated: true, completion: nil)
}

Error:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* Multiple audio/video AVCaptureInputs are not currently supported.'

*** First throw call stack: (0x23f3410b 0x236dae17 0x2946bf73 0x2946b8bf 0x6d0d8 0x6ce28 0x6cebc 0x280a86cd 0x280a8659 0x2809064f 0x280a7fb5 0x28062275 0x280a0e21 0x280a05d3 0x280712f9 0x2806f98b 0x23ef768f 0x23ef727d 0x23ef55eb 0x23e48bf9 0x23e489e5 0x25094ac9 0x280d8ba1 0xa794c 0x23af7873)

libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

like image 892
SwiftDeveloper Avatar asked Feb 12 '16 11:02

SwiftDeveloper


1 Answers

Your setupCaptureSession() method adds a new input each time it's called (with self.captureSession.addInput(deviceInput)).

But the error message explicitly says that "Multiple audio/video AVCaptureInputs are not currently supported".

So you have to use only one input at a time: don't stack them in self.captureSession like you do.

You could remove the previous one (with removeInput) before adding a new one, for example.

To be sure that all inputs are removed before adding a new one, you could do:

if let inputs = captureSession.inputs as? [AVCaptureDeviceInput] {
    for input in inputs {
        captureSession.removeInput(input)
    }
}

just before self.captureSession.addInput(deviceInput).

If it still doesn't work... try something different: instead of removing inputs, change your code so that you add the device input just once, not each time the method is called:

if captureSession.inputs.isEmpty {
    self.captureSession.addInput(deviceInput)
}

instead of just self.captureSession.addInput(deviceInput).

I just tried that in a Playground and it worked. Now that you have understood the idea, it's your responsibility to make it work in your own app by adapting my solutions, I can't do it for you, even if I wanted to... ;)

like image 107
Eric Aya Avatar answered Sep 21 '22 16:09

Eric Aya