【问题标题】:How to put voice captured text into textfield如何将语音捕获的文本放入文本字​​段
【发布时间】:2015-05-18 03:41:09
【问题描述】:

这是一个复杂的问题,但在这里。我构建了一个 jquery 聊天功能,在其中我在文本字段标签中输入一些内容并点击发送,它会进入一个聊天框。就这样完成了。

我刚刚找到了一个使用 javascript 的语音到文本库。 http://ctrlq.org/code/19680-html5-web-speech-api

我的问题是我不知道如何将所有必需的 html 标签放在 textfield 标签内...我认为这不可能...或者我错了。

这是代码目前的工作方式(没有声音)https://jsfiddle.net/v3Lru135/

这是带有语音的代码以及我尝试添加 html 标签https://jsfiddle.net/9r93vcsq/

谁能帮我拿到它,所以当我按下那个按钮并开始说话时,它会将我以文本形式说的话放到文本字段中,这样我就可以点击发送....谢谢

<div id="chatContainer">

     </div>

     <div id="chatControls">

     <!--<textarea id="chatTextBox" placeholder = "Enter your message   
here..."> </textarea>-->

<div id="chatTextBox">
     <div>
      <a href="#" id="start_button"  
onclick="startDictation(event)">Dictate</a>
    </div>

    <div id="results">
      <span id="final_span" class="final"></span>
      <span id="interim_span" class="interim"></span>
    </div>
  </div>


     <button id="chatSend">Send</button>

#chatContainer{
    width: 95%;
    height: 50px;
    background: url(../assets/chatTile.png) repeat;
    border: 1px solid #333;
    margin: 0 auto;
    border-radius: 5px;
    margin-top: 10px;
    overflow-y: scroll !important;
    padding: 5px;
}
#chatTextBox{
    resize: none;
    width: 65%;
    height: 35px !important;
    float: left;
    opacity: .9;
}
#chatControls{
    width: 100%;
    padding-left: 10px;
    padding-right: 10px;
    display: inline-block;
}
#chatSend{
    resize: none !important;
    width: 50%;
    height: 35px !important;
    display: inline-block;
    max-width: 30%;
    float: right;
    opacity: .9;
    padding: 5px;
}


.chatUsername{
    color: red;
    font-weight: bold;
}
.botMan{
    color: #424242;
    font-weight: bold;
}



var canned = ["Ok, how is this?" , "You're welcome!"]

$(function() {

  // grab a reference to the chat
  var chat = $("#chatContainer")

  // add a click handler to send messages
  $("#chatSend").click(function() {

    var username = "<span class=chatUsername>CNN_News: </span>"
      , newMessage = $("#chatTextBox").val() + '<br>'
      , delay = 4000

    // reset the input
    $("#chatTextBox").val("")

    // render the chat
    chat.append(username + newMessage)
    chat.scrollTop(chat.prop("scrollHeight"))

    // set a timeout to send a canned response

    setTimeout(function() {
      chat.append('<span class=botMan>StarkFan: </span>' + 
canned.shift() + '<br>')
      chat.scrollTop(chat.prop("scrollHeight"))
    }, delay)

  // end of click handler
  })
})















var final_transcript = '';
var recognizing = false;

if ('webkitSpeechRecognition' in window) {

  var recognition = new webkitSpeechRecognition();

  recognition.continuous = true;
  recognition.interimResults = true;

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

  recognition.onerror = function(event) {
    console.log(event.error);
  };

  recognition.onend = function() {
    recognizing = false;
  };

  recognition.onresult = function(event) {
    var interim_transcript = '';
    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);

  };
}

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>');
}

function capitalize(s) {
  return s.replace(s.substr(0,1), function(m) { return m.toUpperCase(); });
}

function startDictation(event) {
  if (recognizing) {
    recognition.stop();
    return;
  }
  final_transcript = '';
  recognition.lang = 'en-US';
  recognition.start();
  final_span.innerHTML = '';
  interim_span.innerHTML = '';
}

【问题讨论】:

  • 参见“框架和扩展”jsfiddle.net/26cmcugy/1; jsfiddle 似乎没有将 jQuery 加载到文档中。尝试在 final_transcript 上使用 .text(),在 htmlfinal_transcript 内编译之后?
  • 哦,我的坏我会重做 js 小提琴

标签: javascript jquery html speech-recognition speech-to-text


【解决方案1】:

看来您可以修改网站上给出的示例以满足您的需要:http://www.labnol.org/software/add-speech-recognition-to-website/19989/

  function startDictation() {

    if (window.hasOwnProperty('webkitSpeechRecognition')) {

      var recognition = new webkitSpeechRecognition();

      recognition.continuous = false;
      recognition.interimResults = false;

      recognition.lang = "en-US";
      recognition.start();

      recognition.onresult = function(e) {
         // commented out the old commands, your new command is below these comments
         // document.getElementById('transcript').value = e.results[0][0].transcript;
         // recognition.stop();
         // document.getElementById('labnol').submit();
         $("#chatTextBox").val(e.results[0][0].transcript); // set the input val to the speech transcript
      };

      recognition.onerror = function(e) {
        recognition.stop();
      }

    }
  }

如果这不起作用,我们可以通过打印results的内容来尝试调试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    相关资源
    最近更新 更多