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

别等时光非礼了梦想. 提交于 2019-12-04 06:05:53

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!