AVAudioEngine inputNode installTap 重启录音时崩溃 [英] AVAudioEngine inputNode installTap crash when restarting recording

查看:21
本文介绍了AVAudioEngine inputNode installTap 重启录音时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am implementing Speech Recognition in my app. When I first present the view controller with the speech recognition logic, everything works fine. However, when I try present the view controller again, I get the following crash:

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)'

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

Here is the code used for starting and stopping recording:

@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()
}

}

一旦我们请求授权,就会在 viewDidLoad 中调用

startRecording() .stopRecording() 在视图控制器关闭时调用.

startRecording() is called in viewDidLoad once we have requested authorization. stopRecording() is called when the view controller is dismissed.

请帮忙.我正在努力寻找解决此崩溃的方法

Please assist. I'm struggling to find a solution to this crash

推荐答案

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

First, a small issue. When tapping the device's microphone, you'll want to use the format of the input bus:

let recordingFormat = node.inputFormat(forBus: 0)

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

Second, after some digging it seems like this crash most commonly stems from your application's shared AVAudioSession category settings. Make sure you have your audio session configured like so if you're going to be performing live microphone audio processing:

private func configureAudioSession() {
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: .mixWithOthers)
        try AVAudioSession.sharedInstance().setActive(true)
    } catch { }
}

这篇关于AVAudioEngine inputNode installTap 重启录音时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆