Sometimes, when I am trying to setup the new Speech API to recognize a live audio stream (from microphone as demo'd in the official WWDC video), I sometimes get the following error (on an actual device). It usually happens after a recognition request has already been called.
Recognition error: Optional(Error Domain=kAFAssistantErrorDomain Code=216 "(null)")
Let's assume that I have user permission and that the speech recognizer is available (both are true at the time of the crash). The method I invoke is:
private func listen() throws {
if let recognitionTask = self.recognitionTask {
recognitionTask.cancel()
self.recognitionTask = nil
}
self.recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
guard let recognitionRequest = self.recognitionRequest else { fatalError("Unable to created a SFSpeechAudioBufferRecognitionRequest object") }
guard let inputNode = audioEngine.inputNode else { fatalError("Audio engine has no input node") }
recognitionRequest.shouldReportPartialResults = true
self.recognitionTask = speechRecognizer?.recognitionTaskWithRequest(recognitionRequest, resultHandler: { (result, error) in
print("Error: " + String(error)) // THIS IS THE ERROR
// Code here in which I look for certain phrases - irrelevant
})
let recordingFormat = inputNode.outputFormatForBus(0)
inputNode.installTapOnBus(0, bufferSize: 2048, format: recordingFormat, block: { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
recognitionRequest.appendAudioPCMBuffer(buffer)
})
self.audioEngine.prepare()
try self.audioEngine.start()
}
The error is the one in the -recognitionTaskWithRequest completion handler. Most of these variables are declared or defined outside of the method body:
private let audioEngine = AVAudioEngine()
private var speechRecognizer: SFSpeechRecognizer?
private var recognitionRequest: SFSpeechAudioBufferRecognitionRequest?
private var recognitionTask: SFSpeechRecognitionTask?
I couldn't find an AVFoundation error with the code "216". Any thoughts?