Bluetooth headphones disconnect when AVAudioEngine.inputNode called

时间:2018-05-14 17:30:56

标签: iphone swift4 avaudioplayer avaudioengine

I am trying to implement voice commands that is going to be built into my app. I am able to successfully get it to work with this code:

    let node = audioEngine.inputNode

    let recordingFormat = node.outputFormat(forBus: 0)
    node.installTap(onBus: 0, bufferSize: 1024,
                    format: recordingFormat) {
                        (buffer, _) in
                        self.recognitionRequest!.append(buffer)
    }
    audioEngine.prepare()
    try! audioEngine.start()

Please note that audioEngine is set to AVAudioEngine() in the class.

Although the code works fine, the problem occurs when I have bluetooth headphones connected. The line let node = audioEngine.inputNode prevents my iPhone from connecting and seeing my Bluetooth headphones.

If I remove that line, my headphones connect as expected, however, this means that I cannot speech recognition as that first line is required.

How can I use voice commands and still use my bluetooth headphones?
If it helps, I want the voice commands to listen from the iPhone microphone but I want sound to be played through the headphones. If no headphones are connected, then the sound should play from the iPhone too.

0 个答案:

没有答案