Swift AVCaptureSessionclosures打开button错误:当前不支持多个audio/videoAVCaptureInputs

我有一个工作条码扫描器的代码。 当我点击openCamerabutton时,第一次都是好的。 当我点击closeCamerabutton,好,但如果我再次点击openCamerabutton给出一个致命的错误。 代码和错误如下。 事实上,是否可以用一个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) } 

错误:

*因未捕获exception'NSInvalidArgumentException'而终止应用程序,原因:'*目前不支持多个audio/videoAVCaptureInputs。

***第一掷调用堆栈:(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:以NSException(lldb)types的未捕获exception终止

您的setupCaptureSession()方法每次调用时都会添加一个新input(使用self.captureSession.addInput(deviceInput) )。

但是,错误消息明确表示“目前不支持多个audio/videoAVCaptureInputs”。

所以你一次只能使用一个input:不要像你那样在self.captureSession中堆叠它们。

例如,您可以在添加新的之前删除以前的(使用removeInput )。

为了确保在添加新input之前删除了所有的input,你可以这样做:

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

就在self.captureSession.addInput(deviceInput)之前。

如果它仍然不能工作…尝试一些不同的东西:而不是删除input,更改您的代码,以便您添加设备input一次 ,而不是每次调用方法:

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

而不只是self.captureSession.addInput(deviceInput)

我只是在一个游乐场试图,它的工作。 现在你已经理解了这个想法 ,这是你的责任,通过调整我的解决scheme使它在你自己的应用程序中工作,即使我想……也不能为你做;