Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable camera microphone on AVCapture device input

I am setting a microphone on a AVCaptureSession and I am in need of a switch for the mic. How should I proceed with this? Do I really need to the captureSession?.removeInput(microphone), or is there an easies way?

let microphone = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio)

        do {
            let micInput = try AVCaptureDeviceInput(device: microphone)
            if captureSession.canAddInput(micInput) {
                captureSession.addInput(micInput)
            }
        } catch {
            print("Error setting device audio input: \(error)")
            return false
        }
like image 513
dre_84w934 Avatar asked Jan 08 '18 22:01

dre_84w934


2 Answers

You can always just leave the mic input attached and then using your switch decide what to do with the audio buffer. If the switch is off then don't process the audio data. I found an objc.io article that talks about how to set up the separate audio and video buffers before writing the data with an AVAssetWriter.

like image 170
Walter Avatar answered Sep 30 '22 19:09

Walter


By default, all AVCaptureAudioChannel objects exposed by a connection are enabled. You may set enabled to false to stop the flow of data for a particular channel.

https://developer.apple.com/documentation/avfoundation/avcaptureaudiochannel/1388574-isenabled

like image 23
Santosh Botre Avatar answered Sep 30 '22 19:09

Santosh Botre