当前位置:  开发笔记 > IOS > 正文

重启录制时AVAudioEngine inputNode installTap崩溃

如何解决《重启录制时AVAudioEngineinputNodeinstallTap崩溃》经验,为你挑选了1个好方法。

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

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()视图控制器被关闭时调用.

请协助.我很难找到这个崩溃的解决方案



1> WongWray..:

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

let recordingFormat = node.inputFormat(forBus: 0)

其次,经过一番挖掘之后,似乎这种崩溃通常是由应用程序的共享AVAudioSession类别设置引起的。如果您要执行现场麦克风音频处理,请确保已按照以下方式配置了音频会话:

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

推荐阅读
云聪京初瑞子_617
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有