7

Used the nice code in this tutorial! with some corrections. The speech recognition code is working. But if I trigger the the recognition code more than twice, the error in the title pops up. Hard finding documentation that addresses this. Anyone?

private func recordAndRecognizeSpeech()
    {
        let node = audioEngine.inputNode
        let recordingFormat = node.outputFormat(forBus: 0)
        node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { buffer, _ in
            self.request.append(buffer)
        }

        audioEngine.prepare()
        do {
            try audioEngine.start()
        }
        catch {
            self.sendAlert(message: "There has been an audio engine error.")
            return print (error)
        }

        guard let myRecognizer = SFSpeechRecognizer() else
        {
            self.sendAlert(message: "Speech recognition is not supported for your current locale.")
            return
        }

        if !myRecognizer.isAvailable
        {
            self.sendAlert(message: "Speech recognition is not currently available. Check back at a later time.")
            return
        }

        recognitionTask = speechRecognizer?.recognitionTask(with: request, resultHandler:
        { result, error in
            if result != nil
            {
                if let result = result
                {
                    let bestString = result.bestTranscription.formattedString
                    self.detectedTextLabel.text = bestString
                }

                else if let error = error
                {
                    self.sendAlert(message: "There has been a speech recognition error.")
                    print(error)
                }
            }
        })
    }

The following is the function that starts and stops recognizer.

    /// This button is the toggle for Starting and Stopping the Speech Recognition function
    @objc func didTapSpeechButton()
    {
        if isRecording == true {
            print("--> Stop Recording.")
            request.endAudio()  // Mark end of recording
            audioEngine.stop()
            let node = audioEngine.inputNode
            node.removeTap(onBus: 0)
            recognitionTask?.cancel()
            isRecording = false
            speechButton.backgroundColor = UIColor.red
        } else {
            print("--> Start Recording.")
            self.recordAndRecognizeSpeech()
            isRecording = true
            speechButton.backgroundColor = UIColor.gray
        }
    }
Sid
  • 963
  • 1
  • 12
  • 17
  • The error message is clear. – matt Jan 28 '19 at 06:00
  • I understand the error. My question is where I can find a solution or help. This seems to suggest the [answer](https://stackoverflow.com/questions/45681853/clear-input-of-sfspeechaudiobufferrecognitionrequest-after-every-result-swift3) but new to speech. – Sid Jan 29 '19 at 06:46
  • If you understand the error the solution is obvious. Make a new request object each time. – matt Jan 29 '19 at 08:03

3 Answers3

6

Found a possible answer so decided to share. Others may find it useful. This was added into the function recordAndRecognizeSpeech().

Error was obvious, but the solution not so for us learning. App not crashing. If a better answer exists - hopefully someone else can help.

// This resets the recognitionRequest so "...cannot be re-use..." error is avoided. 
recognitionRequest = SFSpeechAudioBufferRecognitionRequest()   // recreates recognitionRequest object.
guard let recognitionRequest = recognitionRequest else { fatalError("Unable to created a SFSpeechAudioBufferRecognitionRequest object") }
Sid
  • 963
  • 1
  • 12
  • 17
2

This error is related with Error Domain=kAFAssistantErrorDomain Code=216 “(null)”.

One must use finish instead of cancel the recognition.

    // stop recognition
    recognitionTask?.finish()

Find my complete answer here.

wider-spider
  • 465
  • 4
  • 12
0

Just add this code where you want to stop or cancel , Swift 4.2 .

 // stop audio
        request.endAudio()
        audioEngine.stop()

        if audioEngine.inputNode.numberOfInputs > 0 {
            audioEngine.inputNode.removeTap(onBus: 0)
        }
        recognitionTask?.cancel()
LeoNard
  • 230
  • 1
  • 9