问题
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