18

我正在我的应用程序中实现语音识别。当我第一次向视图控制器展示语音识别逻辑时,一切正常。但是,当我再次尝试显示视图控制器时,出现以下崩溃:

ERROR:    [0x190bf000] >avae> AVAudioNode.mm:568: CreateRecordingTap: required condition is false: IsFormatSampleRateAndChannelCountValid(format)
*** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: IsFormatSampleRateAndChannelCountValid(format)'

这是用于开始和停止录制的代码:

@available(iOS 10.0, *)
extension DictationViewController {

fileprivate func startRecording() throws {
    guard let recognizer = speechRecognizer else {
        debugLog(className, message: "Not supported for the device's locale")
        return
    }

    guard recognizer.isAvailable else {
        debugLog(className, message: "Recognizer is not available right now")
        return
    }

    mostRecentlyProcessedSegmentDuration = 0
    guard let node = audioEngine.inputNode else {
        debugLog(className, message: "Could not get an input node")
        return
    }

    let recordingFormat = node.outputFormat(forBus: 0)
    node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { [weak self] (buffer, _) in
        self?.request.append(buffer)
    }

    audioEngine.prepare()
    try audioEngine.start()

    recognitionTask = recognizer.recognitionTask(with: request, resultHandler: {/***/})
}

fileprivate func stopRecording() {
    audioEngine.stop()
    audioEngine.inputNode?.removeTap(onBus: 0)
    request.endAudio()
    recognitionTask?.cancel()
}

}

startRecording()一旦我们请求授权,就会在 viewDidLoad 中调用。stopRecording()当视图控制器被关闭时调用。

请协助。我正在努力寻找解决此崩溃的方法

4

6 回答 6

21

首先,一个小问题。点击设备的麦克风时,您需要使用输入总线的格式:

let recordingFormat = node.inputFormat(forBus: 0)

其次,经过一番挖掘,似乎这种崩溃最常见的原因是您的应用程序的共享 AVAudioSession 类别设置。如果您要执行实时麦克风音频处理,请确保您的音频会话配置如下:

private func configureAudioSession() {
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: .mixWithOthers)
        try AVAudioSession.sharedInstance().setActive(true)
    } catch { }
}
于 2017-12-20T09:17:40.117 回答
14

有两种可能的方法来解决这个问题。

  1. 检查inputFormat.channelCount。它可能会抛出错误,因为麦克风正在另一个应用程序或您的其他地方使用。
if(inputNode.inputFormat(forBus: 0).channelCount == 0){
    NSLog("Not enough available inputs!")
    return
}
  1. 尝试重置audioEngine.
audioEngine.reset()
于 2019-08-14T00:42:39.423 回答
5

您可以替换此代码:

let recordingFormat = node.outputFormat(forBus: 0)

具有以下内容:

let recordingFormat = AVAudioFormat(standardFormatWithSampleRate: 44100, channels: 1)

这段代码解决了这个问题。

于 2017-08-09T18:32:38.007 回答
3

required condition is false: IsFormatSampleRateAndChannelCountValid(format)在接听电话时尝试使用语音识别时,我遇到了崩溃,这导致采样率为零。我的解决方案是创建下面的audioInputIsBusy()函数并在try audioSession.setCategory(.record, mode: .measurement, options: .duckOthers) 它阻止崩溃之前调用它,我显示一条消息“语音识别不可用”,然后用audioEngine = AVAudioEngine().

func audioInputIsBusy(recordingFormat: AVAudioFormat) -> Bool {
    guard recordingFormat.sampleRate == 0 || recordingFormat.channelCount == 0 else {
        return false
    }

    return true
}

ps:let recordingFormat = audioEngine.inputNode.outputFormat(forBus: 0)

于 2020-06-18T02:07:04.377 回答
2

我必须调用removeTap()之前的 installTap 才能使其工作。上述解决方案都不适合我。

//Remove tap first.
inputNode.removeTap(onBus: 0)

// Configure the microphone input.
let recordingFormat = inputNode.inputFormat(forBus: 0)            
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
            //process buffer...
        }
于 2021-01-05T18:39:53.963 回答
0

在每次开始运行之前试试这个:

音频引擎 = AVAudioEngine()

于 2020-10-12T18:18:27.180 回答