webkitSpeechRecognition “滞后"收集结果时落后 [英] webkitSpeechRecognition is "lagging" behind when gathering results

查看:70
本文介绍了webkitSpeechRecognition “滞后"收集结果时落后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很想尝试 Web Speech API.我完全从文章中复制了代码,我在您说话时遇到了问题,但在您再次说话之前没有任何反应.

Had an itch to try out the Web Speech API. I copied the code exactly from the article, and I'm having an issue where you speak, but nothing happens until you speak AGAIN.

[小提琴:http://jsfiddle.net/w75v2tm5/]

JS:

if (!('webkitSpeechRecognition' in window)) {
    //handle error stuff here...
} else {
    var recognition = new webkitSpeechRecognition();
    recognition.continuous = true;
    recognition.interimResults = false;

    recognition.start();

    var final_transcript = '';

    recognition.onresult = function (event) {
        var interim_transcript = '';
        if (typeof (event.results) == 'undefined') {
            recognition.onend = null;
            recognition.stop();
            upgrade();
            return;
        }
        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;
            }
        }
        document.getElementsByTagName('div')[0].innerText = final_transcript;
    };

}

例如,如果我要说Hello world",<div>我已经设置了显示结果不会显示Hello world",直到我说了别的东西,或者发出了声音.但是,如果我说其他的话,那将不会显示,直到我再说一遍.

For example, if I were to say "Hello world", the <div> I have set up to display the results would not display "Hello world" until I said something else, or made a sound. But if I said something else, THAT would not be displayed until I said something else AGAIN.

变量final_transcript"保存的是先前的结果,而不是我刚才所说的.仅差 1 点.

The variable "final_transcript" is holding the PREVIOUS result, and not what I just said. It's off by just 1.

为了给你一个更好的主意...

To give you a better idea...

我:你好世界"

final_transcript = '';

final_transcript = '';

[等等...]

我:测试"

final_transcript = 'Hello world'

final_transcript = 'Hello world'

而这只是继续.代码无法像我所说的那样转录我所说的内容.很奇怪.

And this just continues. The code is failing to transcribe what I am saying AS I am saying it. Very weird.

有没有想过为什么会这样?

Any thoughts as to why this could be?

推荐答案

有一些内置的超时,之后即使没有更多的输入你也会得到结果(似乎在 5-10 秒左右).

There is some kind of built in timeout, after which you will get the result even if there is no more input (seems to be around 5-10 seconds).

在这种情况下,您将获得最终的 onresult 事件以及 onend 事件.如果您希望继续接受输入,则必须再次调用 recognition.start().

In this case you will get the final onresult event, as well as the onend event. You will have to call recognition.start() again if you wish to keep accepting input.

另外,如果你设置了

recognition.interimResults = true;

您将获得具有非最终结果的 onresult 事件,并且您可以在获得最终结果之前决定是否要显示它们.

you will get onresult events with non final results, and you can decide if you want to display them before you get the final ones.

另一个选项是关闭连续

recognition.continuous = false;

您将在输入(音频)停止后不久得到结果.您还将获得 onend 事件.
如果您想继续认可,您必须再次致电

you will get a result shortly after the input (audio) stopped. You will also get the onend event.
If you wish to continue the recognition you will have to call again

recognition.start();

onend 事件处理程序中.
在非 HTTPS 页面上,这将导致再次弹出权限栏.

in the onend event handler.
On a non HTTPS page, this will cause the permission bar to pop up again.

参见示例

这篇关于webkitSpeechRecognition “滞后"收集结果时落后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆