IsFormatSampleRateAndChannelCountValid false when playing outside audio

My app listens for verbal commands "Roll" & "Skip". It was working well until I used it while listening to a podcast in another app. I am getting a crash with the error: Thread 1: "required condition is false: IsFormatSampleRateAndChannelCountValid(format)" .

It crashes when I am playing audio from the apps Snipd (a podcast app) or the Apple Podcast app.

When I am playing audio from Youtube or the Apple Music it does not crash.

This is the code for when I start listening for the commands:

    // MARK: - Speech Recognition
    func startListening() {
        do {
            try configureAudioSession()
            createRecognitionRequest()
            try prepareAudioEngine()
        } catch {
            print("Audio Engine error: \(error.localizedDescription)")
        }
    }

    private func configureAudioSession() throws {
        let audioSession = AVAudioSession.sharedInstance()
        try audioSession.setCategory(.playAndRecord, mode: .measurement, options: [.interruptSpokenAudioAndMixWithOthers, .duckOthers])
        try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
    }
    
    private func createRecognitionRequest() {
        recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
        guard let recognitionRequest = recognitionRequest else { return }

        recognitionRequest.shouldReportPartialResults = true
        recognitionTask = speechRecognizer?.recognitionTask(with: recognitionRequest, resultHandler: handleRecognitionResult)
    }
    
    private func prepareAudioEngine() throws {
        let inputNode = audioEngine.inputNode
        inputNode.removeTap(onBus: 0)

        let inputFormat = inputNode.inputFormat(forBus: 0)
        inputNode.installTap(onBus: 0, bufferSize: 1024, format: inputFormat) { [weak self] (buffer, _) in
            self?.recognitionRequest?.append(buffer)
        }
        audioEngine.prepare()
        try audioEngine.start()
        isActuallyListening = true
    }

Thanks

Hello, thanks for taking the time to write about this issue, sorry you're running into this! Please take a few extra minutes and paste this issue into a bug report so we can confirm this is tracked in our internal system. Doing this will help us get vital information about the issue like crash logs from your device. You can file a report here: https://developer.apple.com/bug-reporting/

Once you do, feel free to reply with the Feedback ID number and I can make sure the right people are aware of this issue.

Got the same issue. Did anyone solve it?

IsFormatSampleRateAndChannelCountValid false when playing outside audio
 
 
Q