webkitSpeechRecognition不显示中期结果

时间:2015-06-23 05:18:04

标签: javascript google-chrome webkit speech-recognition

我正在尝试按照HERE实现此语音识别服务,并使用与THIS示例类似的INTERIM(在最终结果之前交替的文本)显示结果。您可以看到文本在着陆到最终结果之前交替显示。我正在关注的教程(第一个链接)给出了一个实现此功能的示例,但它只是给了我最终答案,并没有显示单词迭代动画。

  <span id="final_span"></span>
   <span id="interim_span" style="color: grey"></span>

   <button id="button" type="button" onclick="toggleStartStop()"></button>

和Javascript:

 <script type="text/javascript">
                var recognizing;
                var recognition = new webkitSpeechRecognition();
                recognition.continuous = true;
                recognition.interim = true;
                reset();
                recognition.onend = reset;

                recognition.onresult = function (event) {
                    var final = "";
                    var interim = "";
                    for (var i = 0; i < event.results.length; ++i) {
                        if (event.results[i].final) {
                            final += event.results[i][0].transcript;
                        } else {
                            interim += event.results[i][0].transcript;
                        }
                    }
                    final_span.innerHTML = final;
                    interim_span.innerHTML = interim;
                }

                function reset() {
                    recognizing = false;
                    button.innerHTML = "Click to Speak";
                }

                function toggleStartStop() {
                    if (recognizing) {
                        recognition.stop();
                        reset();
                    } else {
                        recognition.start();
                        recognizing = true;
                        button.innerHTML = "Click to Stop";
                        final_span.innerHTML = "";
                        interim_span.innerHTML = "";
                    }
                }
            </script>

我做错了什么?我需要创建与THIS

完全相同的东西

1 个答案:

答案 0 :(得分:0)

根据documentation

你需要

    recognition.interimResults = true;

与当前来源不一样

    recognition.interim = true;

如果您检查链接页面的源代码,也可以弄明白。