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