AVAudioEngine inputNode installTap crash when restarting recording

后端 未结 5 1981
猫巷女王i
猫巷女王i 2020-12-14 18:59

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 presen

5条回答
  •  Happy的楠姐
    2020-12-14 19:37

    I was getting the required condition is false: IsFormatSampleRateAndChannelCountValid(format) crash when attempting to use speech recognition while taking a phone call, which caused the sample rate to equal zero. My solution was to create the below audioInputIsBusy() function and call it before try audioSession.setCategory(.record, mode: .measurement, options: .duckOthers) That prevented the crash and I displayed a message that the "speech recognition is unavailable" and then reset the audioEngine with 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)

提交回复
热议问题