通过AVAudioEngine使用电话麦克风作为输入并使用AirPods /耳机作为输出

时间:2019-11-19 13:45:06

标签: ios swift core-audio avaudiosession airpods

正如标题所述,我需要将音频数据从手机的麦克风路由到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...
})

0 个答案:

没有答案