首页 > 解决方案 > 用 JavaScript 覆盖文本框语音到文本

问题描述

通过网络语音 API,我使用 html 和 javascript 将语音转换为文本。每次说话时如何覆盖文本框?对于当前时刻,每次我说新的下一个删除上一个。如何在说话后保留文本框中的所有文本而不删除以前的文本?

    var btnSpeechEnfSig = document.getElementById('btnSpeechEnfSig');

    btnSpeechEnfSig.onclick = function() {
    var output = document.getElementById("enf_signopsis");
    // get action element reference
    var action = document.getElementById("actionSpeechEnfSig");
    runSpeechRecognition(output,action);
    }


    function runSpeechRecognition(output,action) {

    var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
    var recognition = new SpeechRecognition();

    // This runs when the speech recognition service starts
    recognition.onstart = function() {
    action.innerHTML = "<small>escuchando, habla...</small>";
    };

    recognition.onspeechend = function() {
    action.innerHTML = "<small>grabación terminó...</small>";
    recognition.stop();
    
    }

    // This runs when the speech recognition service returns result
    recognition.onresult = function(event) {
    var transcript = event.results[0][0].transcript;
    
    output.value += transcript;
  }
    };
    <button type="button" id="btnSpeechEnfSig"  ><i class="fa fa-microphone""></i></button>
    <span id="actionSpeechEnfSig"></span>
    <textarea rows="6"  id="enf_signopsis" ></textarea>

标签: javascripthtml

解决方案


使用output.value += transcript;.


推荐阅读