简体   繁体   English

Web 语音 api 在几秒钟后关闭

[英]Web speech api closes after some seconds

I'm using web speech api我正在使用 web 语音 api

https://www.google.com/intl/en/chrome/demos/speech.html https://www.google.com/intl/en/chrome/demos/speech.html

but mic automatically closes after some seconds but i have to close mix only when the user clicks on close buttons.但是麦克风会在几秒钟后自动关闭,但只有当用户单击关闭按钮时我才必须关闭混音。

Any solution to resolve this issue.解决此问题的任何解决方案。 Thanks谢谢

You should mark the recognition service as continuous and maybe start the recorder again if it stops after the timeout when there is no activity.您应该将识别服务标记为连续,如果在没有活动的情况下超时后停止,则可以重新启动记录器。

 <button onclick='toggleRecording()'>Toggle recorder</button>
 <div id='results'></div>
 <script>
    window.SpeechRecognition = window.SpeechRecognition ||
    window.webkitSpeechRecognition;

    let recognition = new window.SpeechRecognition()
    let recording = false;
    let results = null;

    recognition.continuous = true;

    function toggleRecording() {
        if(recording) {
            recognition.onend = null;
            recognition.stop();
            recording = false;

            // Printing all results we got so far.
            if(results) {
                let resultsDiv = document.getElementById('results')
                for(let i=0; i<results.length; ++i)
                    resultsDiv.innerHTML = resultsDiv.innerHTML + results.item(i)[0].transcript
            }
        } else {
            recognition.onend = onEnd;
            recognition.start();
            recording = true;
        }
    }

    function onEnd() {
        console.log('Speech recognition has stopped. Starting again ...');
        recognition.start();
    }


    function onSpeak(e) {
        results = e.results;
        console.log(e.results[e.results.length-1][0].transcript);
    }

    recognition.addEventListener('result', onSpeak);

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM