4

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

0 回答 0