在硬件上 运行 时没有可用的音频引擎输入通道

No available audioEngine input channels when running on hardware

我正在尝试在我的音频引擎 inputNode 上设置一个点击以进行语音识别。以下代码在模拟器中运行良好,但当我在 iPhone X 运行ning iOS 13.1.2.

上 运行 时却不行

相关代码如下:

if (audioEngine == nil || (!audioEngine!.isRunning)) {
    print("Setting up audioEngine")
    if audioEngine == nil {
        audioEngine = AVAudioEngine.init()
    }

    if(audioEngine!.inputNode.inputFormat(forBus: 0).channelCount == 0){
        print("Not enough available inputs!")
    }

    let recognitionFormat = audioEngine!.inputNode.inputFormat(forBus: 0)
    audioEngine!.inputNode.installTap(onBus: 0, bufferSize: 1024, format: recognitionFormat) {
        (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
        self.recognitionRequest?.append(buffer)
    }
    audioEngine?.prepare()
    try audioEngine?.start()
}

我在模拟器中 运行ning 没有错误,语音识别工作正常。但是,在我的代码上 运行ning 时,我得到了以下信息:


Setting up audioEngine
[aurioc] AURemoteIO.cpp:1086:Initialize: failed: -10851 (enable 1, outf< 2 ch,      0 Hz, Float32, non-inter> inf< 2 ch,      0 Hz, Float32, non-inter>)
[aurioc] AURemoteIO.cpp:1086:Initialize: failed: -10851 (enable 1, outf< 2 ch,      0 Hz, Float32, non-inter> inf< 2 ch,      0 Hz, Float32, non-inter>)
Not enough available inputs!

如果我在 channelCount == 0 之后不 return,应用程序将崩溃:

[avae]            AVAEInternal.h:76    required condition is false: [AVAEGraphNode.mm:823:CreateRecordingTap: (IsFormatSampleRateAndChannelCountValid(format))]
*** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: IsFormatSampleRateAndChannelCountValid(format)'

我的音频会话设置如下:

try audioSession.setCategory(.playAndRecord, options: [.defaultToSpeaker])
try audioSession.setActive(true)

检查可用的输入结果:

LIST OF INPUTS: [<AVAudioSessionPortDescription: 0x280eac6b0, type = MicrophoneBuiltIn; name = iPhone Microphone; UID = Built-In Microphone; selectedDataSource = Bottom>]

我被难住了。有帮助吗?

我想通了。问题与 Flutter 有关(此应用程序正在使用它,但我认为不相关。)事实证明它是相关的。显然 FlutterAppDelegate@application 方法不在主队列上 运行,这是我开始 AudioSession 的地方。当我这样开始 AudioSession 时,我的问题就消失了:

DispatchQueue.main.async {
    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(.playAndRecord, mode: .spokenAudio, options: .defaultToSpeaker)
        try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
    } catch {
        print("Failed to setup audio session")
    }
}