【问题标题】:Web speech api closes after some secondsWeb 语音 api 在几秒钟后关闭
【发布时间】:2022-01-10 14:18:30
【问题描述】:

我正在使用网络语音 api

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

但麦克风会在几秒钟后自动关闭,但我只有在用户点击关闭按钮时才需要关闭混音。

解决此问题的任何解决方案。 谢谢

【问题讨论】:

    标签: javascript speech-recognition speech-to-text webspeech-api


    【解决方案1】:

    您应该将识别服务标记为连续,如果在没有活动的情况下超时后停止,则可以重新启动记录器。

     <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>
    

    【讨论】:

    • 感谢我检查过的答案,在授予麦克风许可后,它再次请求许可,但我想连续录制直到用户按下停止按钮,然后打印整个会话结果你能请更新你的答案
    • @user3653474 嗯,是的,当您不在未通过 HTTPS 或 localhost 提供的页面上运行脚本时,这是预期的行为。 developers.google.com/web/updates/2013/01/…
    • 好的,我将如何打印一个会话中记录的最终结果,从开始录制到按下按钮结束
    • @user3653474 我已经更新了答案,以便它打印所有获得的结果,直到记录停止。
    • 好的,谢谢它的工作
    猜你喜欢
    • 1970-01-01
    • 2022-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    相关资源
    最近更新 更多