Patrick
Patrick

Reputation: 66

How to end a SFSpeechRecognitionTask after a few second of silence

I'm working with the new Apple Speech library and I can't find a command to flip the isFinal bool and end the transcription after a few seconds of silence. Does anyone know how to end the task after a bit of silence?

Here is the code which I receive speech transcription results.

    recognitionRequest.shouldReportPartialResults = true

    speechRecogTask = speechRecognizer.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in
        var isFinal = false
        if result != nil {

            self.textField.text = result?.bestTranscription.formattedString
            isFinal = (result?.isFinal)!
        }

        if error != nil || isFinal {
            self.audioEngine.stop()
            inputNode.removeTap(onBus: 0)

            self.speechRecogRequest = nil
            self.speechRecogTask = nil
        }
    })

Any ideas?

Upvotes: 3

Views: 3090

Answers (2)

Christos
Christos

Reputation: 1

if let result = result {
                
  if (isFinal == true) {
    self.speechRecogTask?.finish()
  }

}

Upvotes: 0

Devang
Devang

Reputation: 320

Your best bet would be to use a timer to detect the interval when the last delegate was invoked. Have a look at this library which is using a NSTimer

Upvotes: 3

Related Questions