正如标题所述,我需要将音频数据从手机的麦克风路由到AirPods或其他具有AVAudioEngine的连接耳机。我已经能够从AirPods麦克风获得输入的这种工作方式,但是当我将AVAudioSession的首选输入源设置为内置iPhone麦克风时,没有任何输出。
我的代码的一些相关摘要:
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, options: [.allowBluetooth])
try AVAudioSession.sharedInstance().setMode(.voiceChat)
if let phoneMicIndex = AVAudioSession.sharedInstance().availableInputs?.firstIndex(where: { $0.portType == .builtInMic }) {
try AVAudioSession.sharedInstance().setPreferredInput(AVAudioSession.sharedInstance().availableInputs?[phoneMicIndex])
}
self.audioEngine.connect(self.audioEngine.inputNode, to: drc, format: self.audioEngine.inputNode.outputFormat(forBus: Config.bus))
self.audioEngine.connect(drc, to: self.audioEngine.mainMixerNode, format: self.audioEngine.outputNode.outputFormat(forBus: Config.bus))
// drc is just an audio unit...
self.audioEngine.inputNode.installTap(onBus: Config.bus, bufferSize: AVAudioFrameCount(Config.bufferSize), format: self.audioEngine.inputNode.inputFormat(forBus: Config.bus), block: { (buffer: AVAudioPCMBuffer, time: AVAudioTime) in
// I get audio data here, it just isn't played out via the AirPods...
})