Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to stop and restart web speech api correctly?

Does it required to create new instance of SpeechRecognition after each speech?

var recognition = new SpeechRecognition();
recognition.start();

Or just stop() and call the start() func again?

recognition.stop();
recognition.start();
like image 373
complez Avatar asked Mar 20 '17 04:03

complez


People also ask

How do you stop a JavaScript speech?

cancel() The cancel() method of the SpeechSynthesis interface removes all utterances from the utterance queue. If an utterance is currently being spoken, speaking will stop immediately.

Can I use Web Speech API?

Support for Web Speech API speech recognition is currently limited to Chrome for Desktop and Android — Chrome has supported it since around version 33 but with prefixed interfaces, so you need to include prefixed versions of them, e.g. webkitSpeechRecognition .

What is window webkitSpeechRecognition?

The speech recognition interface lives on the browser's window object as SpeechRecognition in Firefox and as webkitSpeechRecognition in Chrome. Start by setting the recognition interface to SpeechRecognition (regardless of the browser) using: window. SpeechRecognition = window. webkitSpeechRecognition || window.


1 Answers

Only 1 instance is necessary to interact with the SpeechRecognition object.

You can start the listener with start(). You can stop the listener with stop() or abort().

The abort() method slightly differs from the stop method:

The abort() method of the Web Speech API stops the speech recognition service from listening to incoming audio, and doesn't attempt to return a SpeechRecognitionResult.

Here is the example straight from the docs:

var recognition = new SpeechRecognition();
var speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;

var diagnostic = document.querySelector('.output');
var bg = document.querySelector('html');

document.body.onclick = function() {
  recognition.start();
  console.log('Ready to receive a color command.');
}

abortBtn.onclick = function() {
  recognition.abort();
  console.log('Speech recognition aborted.');
}

recognition.onspeechend = function() {
  recognition.stop();
  console.log('Speech recognition has stopped.');
}

Find out more from the SpeechRecognition docs.

like image 92
Isaac Pak Avatar answered Oct 31 '22 05:10

Isaac Pak