Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SFSpeechAudioBufferRecognitionRequest cannot be re-used

Tags:

swift

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
        }
    }
like image 992
Sid Avatar asked Jan 28 '19 05:01

Sid


1 Answers

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") }
like image 168
Sid Avatar answered Sep 30 '22 18:09

Sid