Swift AVCaptureSession关闭打开按钮错误:当前不支持多个音频/视频AVCaptureInputs [英] Swift AVCaptureSession Close Open Button Error : Multiple audio/video AVCaptureInputs are not currently supported

查看:135
本文介绍了Swift AVCaptureSession关闭打开按钮错误:当前不支持多个音频/视频AVCaptureInputs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有效的条形码扫描仪代码.当我单击openCamera按钮时,第一次一切都很好.当我单击closeCamera按钮时,很好,但是如果再次单击openCamera按钮,则会出现致命错误.代码和错误如下.实际上,是否可以通过一个按钮切换摄像机视图?

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)
}

错误:

*由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* 当前不支持多个音频/视频AVCaptureInput.'

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

***首先抛出调用堆栈: (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)

*** 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:以类型为NSException的未捕获异常终止 (lldb)

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

推荐答案

您的setupCaptureSession()方法每次调用时(使用self.captureSession.addInput(deviceInput))添加新输入.

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

但是错误消息明确指出当前不支持多个音频/视频AVCaptureInputs".

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

因此,您一次只能使用一个输入:不要像您一样将它们堆叠在self.captureSession中.

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

例如,可以在添加新的之前删除前一个(使用removeInput).

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)
    }
}

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)
}

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

我只是在一个操场上尝试过,它奏效了.既然您已经了解了 idea ,那么您有责任通过调整我的解决方案使其在自己的应用中运行,即使我想...,我也无法为您做这件事;;)

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... ;)

这篇关于Swift AVCaptureSession关闭打开按钮错误:当前不支持多个音频/视频AVCaptureInputs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆