WebkitSpeechRecognition随机停止录制

时间:2016-01-15 19:14:17

标签: javascript webkitspeechrecognition

我正在尝试对文本转录进行连续发言,但似乎无法让webkitSpeechRecognition像宣传的那样工作。它似乎随机停止录制(通常在长时间的静音之后),即使我设置了continuous = true和interimResults = true。我甚至无法弄清楚导致这种随机停止的原因,因为我在EVERY.SINGLE.EVENT中添加了一个简单的日志语句。 webkitSpeechRecognition基于我能找到的文档。这是我所拥有的剧本(基本上是演示https://www.google.com/intl/en/chrome/demos/speech.html上的轻微模式)

我所知道的:

录制前不会记录任何错误。

我正处于千兆连接状态,因此与延迟无关。

长时间停顿后会正常停止,但有时会无故停止。

可能在2分钟或30秒后发生。

我评论了下面的一些内容,但我已经尝试了所有这些,但没有成功跟踪问题。

    var final_transcript = '';
    var recognizing = false;
    var ignore_onend;
    var start_timestamp;
    if (!('webkitSpeechRecognition' in window)) {
        upgrade();
    } else {
        var recognition = new webkitSpeechRecognition();
        recognition.continuous = true;
        recognition.interimResults = true;

        recognition.onstart = function () {
            recognizing = true;
        };

        recognition.onerror = function (event) {
            console.log("ERROR")
            recognizing = false
            recognition.stop()
            recognizing = true
            recognition.start()
            if (event.error == 'no-speech') {
                console.log("NO SPEECH")
            }
            if (event.error == 'audio-capture') {
                console.log("Capture Problem")
            }
            if (event.error == 'not-allowed') {
                if (event.timeStamp - start_timestamp < 100) {
                    console.log("Block")
                } else {
                    console.log("Deny")
                }
            }
        };

        recognition.onend = function () {
            console.log("ONEND")
            recognition.stop()
            recognizing = false
            recognition.start()
        };

        recognition.onresult = function (event) {
            var interim_transcript = '';
            if (typeof(event.results) == 'undefined') {
                  console.log("undefined start")
                  recognition.stop()
                  recognizing = false
                  recognition.start()
                  console.log("undefined end")
                  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;
                }
            }
            final_transcript = capitalize(final_transcript);
            final_span.innerHTML = linebreak(final_transcript);
            interim_span.innerHTML = linebreak(interim_transcript);
        };

{#            recognition.onspeechend = function () {#}
{#                console.log("SpeechEND")#}
{#            };#}
{##}
{#            recognition.onpause = function() {#}
{#                console.log("PAUSE")#}
{#            }#}
{##}
{#            recognition.onsoundend = function() {#}
{#                console.log("Sound")#}
{#            }#}
{##}
{##}
{#            recognition.onaudioend = function() {#}
{#                console.log("AUDIO")#}
{#            }#}
{##}
{##}
{#            recognition.onnomatch = function() {#}
{#                console.log("NOMATCH")#}
{#            }#}
{##}
{#            recognition.onmark = function() {#}
{#                console.log("MARK")#}
{#            }#}
{##}
{#            recognition.onboundary = function(){#}
{#                console.log("BOUNDARY")#}
{#            }#}

        }
    var two_line = /\n\n/g;
    var one_line = /\n/g;
    function linebreak(s) {
        return s.replace(two_line, '<p></p>').replace(one_line, '<br>');
    }

    var first_char = /\S/;
    function capitalize(s) {
        return s.replace(first_char, function (m) {
            return m.toUpperCase();
        });
    }

    $("#start_call").click(function () {
        $("#start_call").addClass('hidden');
        $("#end_call").removeClass('hidden');
        final_transcript = '';
        recognition.lang = 'en-US';
        recognition.start();
        ignore_onend = false;
    });
    $("#end_call").click(function () {
        $("#end_call").addClass('hidden');
        $("#start_call").removeClass('hidden');
        stopwatchClock.addClass('hidden');
        recognition.stop();
        recognizing = false
    });

1 个答案:

答案 0 :(得分:1)

您可以在我的github页面https://github.com/pantprateek/genieYT中检查文件player.html中的示例实现。逻辑如下:

创建一个计时器,每10秒后停止识别。

    setInterval(resetVoiceRecog, 10000);
    function resetVoiceRecog() {
       recognition.stop();
    }

当识别.stop被调用时,它肯定会调用onend然后再次开始识别。

recognition.onend = function(event) {
      recognition.start();
   }

即使我不说一句话,这种方法也适合我几个小时。 可能有帮助。