无法收集结果HTML5语音识别API

时间:2014-08-18 01:33:16

标签: javascript html5 speech-recognition

我正在使用语音识别API,但无论何时完成,我都无法检索结果。

以下是我正在使用的代码片段:

    var recognition = new webkitSpeechRecognition();
    recognition.continuous = true;
    recognition.interimResults = true;
    recognition.onresult = function(event) 
    { 
        console.log(event) 
    }

此输出(在控制台中):

    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

我无法绕过它。如何检索结果?

1 个答案:

答案 0 :(得分:1)

结果位于您返回的事件的results列表中。您可以在此处找到要访问的示例代码:

http://updates.html5rocks.com/2013/01/Voice-Driven-Web-Apps-Introduction-to-the-Web-Speech-API

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);
  };
}