user2081044 user2081044 - 7 months ago 72
Javascript Question

Unable to collect results HTML5 speech recognition API

I am using the speech recognition API but I am unable to retrieve the results whenever it's done.

Here is the piece of code that I am using:

var recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.onresult = function(event)

This outputs (in console):

bubbles: false
cancelBubble: false
cancelable: false
clipboardData: undefined
currentTarget: SpeechRecognition
defaultPrevented: false
emma: null
eventPhase: 0
interpretation: null
path: NodeList[0]
resultIndex: 0
results: SpeechRecognitionResultList
returnValue: true
srcElement: SpeechRecognition
target: SpeechRecognition
timeStamp: 1408325350996
type: "result"
__proto__: SpeechRecognitionEvent

I just can't wrap my head around it. How do I retrieve the results?


The result is in the results list in your returned event. You can find the sample code to access it here:

recognition.onresult = function(event) {
    var interim_transcript = '';

    for (var i = event.resultIndex; i < event.results.length; ++i) {
      if (event.results[i].isFinal) {
        final_transcript += event.results[i][0].transcript;
      } else {
        interim_transcript += event.results[i][0].transcript;
    final_transcript = capitalize(final_transcript);
    final_span.innerHTML = linebreak(final_transcript);
    interim_span.innerHTML = linebreak(interim_transcript);