Search code examples
javascriptjqueryspeechwebspeech-apiwebkitspeechrecognition

Web Speech API - Speak again (reset sentence)


i use the webkitSpeechRecognition() to create my own little voice recorder.

Here is a demo (works only with chrome!)
http://jsfiddle.net/gsu4aod2/

Say a sentence and
1a.) if the text correct, click on the "add sentence" button
1b.) if the text not correct click the "speak again" button

My problem is that i dont know how to "reset" the text. When i use the stop() and start() method, the text is removed but than i must confirm the access to the microphone again and again.

Any ideas?

Web Speech API Specification https://www.google.com/intl/en/chrome/demos/speech.html


Solution

  • You are getting the events.result data starting from index 0. You need to start the for loop with something called event.resultIndex.

    resultIndex attribute: The resultIndex must be set to the lowest index in the "results" array that has changed. [source]

    recognition.onresult = function (event) {
        //console.log(event);
          var final = "";
          var interim = "";
          for (var i = event.resultIndex; i < event.results.length; ++i) {
            if (event.results[i].final) {
              final += event.results[i][0].transcript;
            } else {
              interim += event.results[i][0].transcript;
            }
          }
    

    Fiddle Demo