How to select audio input device (mic) in AVAudioEngine on macOS / swift?

空扰寡人 提交于 2020-01-25 08:03:45

问题


Is it possible to select the input device in AVAudioEngine using Swift on macOS?

Use case:

I am using SFSpeechRecognizer on macOS.

To feed microphone data into it I am using

private let audioEngine = AVAudioEngine()
:

let inputNode = audioEngine.inputNode
let recordingFormat = inputNode.outputFormat(forBus: 0)

inputNode.installTap( onBus: 0,  bufferSize: 1024,  format: recordingFormat )
{ (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
    self.recognitionRequest?.append( buffer )
}

audioEngine.prepare()
try audioEngine.start()

This will use the system default microphone.

I have an external USB microphone I wish to use instead. I can go into System Preferences -> Audio and set the default output device to my USB microphone. Then it will work. But if I disconnect the microphone and reconnect it to a different USB port, I will have to go through the process of setting it as default again.

To avoid having to do this repeatedly, I would like to set the microphone manually from code.

Is this possible?

EDIT: I found Set AVAudioEngine Input and Output Devices which uses Obj-C.

来源:https://stackoverflow.com/questions/58893935/how-to-select-audio-input-device-mic-in-avaudioengine-on-macos-swift

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