javascript - 如何让语音识别持续固定时间段?

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

我想让我的语音识别 JavaScript 在固定时间段内连续录制,比如说 5 分钟。

我的js有以下代码

try {
  var SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
  var recognition = new SpeechRecognition();
}
catch(e) {
  // console.error(e);
  $('.no-browser-support').show();
  //$('.app').hide();
}

var noteTextarea = $('#note-textarea');
var instructions = $('#recording-instructions');
var notesList = $('ul#notes');

var noteContent = '';

// Get all notes from previous sessions and display them.
var notes = getAllNotes();
renderNotes(notes);


$( document ).ready(function() {
  // Handler for .ready() called.
   if (noteContent.length) {
      noteContent += ' ';
   }

   recognition.start();

});

最佳答案

SpeechRecognition 接口(interface)的 onend 属性表示一个事件处理程序,该事件处理程序将在语音识别服务断开连接时(当结束事件触发时)运行。因此,当此事件发生时,您可以使用 start()

再次启动 SpeechRecognition

所以,最后你应该有类似的东西

var recognition = new SpeechRecognition();
 ...

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

要使其持续五分钟,您可以使用计时器等,例如 setInterval 并在 onend 回调中添加检查以检查是否启动识别 再次或不。类似的东西

var counter = 0;
var interval = setInterval(function(){
      counter++;
},1000)


recognition.onend = function() {
 if(counter <= 5 * 60)
  recognition.start();
 else
   clearInterval(interval)
}

请注意,识别会在 5 分钟后停止,但至少持续 5 分钟

MDN

关于javascript - 如何让语音识别持续固定时间段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51080738/

相关文章:

javascript - 在 jQuery 中添加带有单引号的 HTML 代码

javascript - 刷新页面和在 webkit 浏览器中输入页面有什么区别吗?(棘手)

java - CMUSphinx 永远无法识别音频文件中的任何单词

javascript - 基于设备的链接重定向

jquery - 当用户滚动到我的单页网站的各个部分时,激活导航栏中的元素

java - 如何在 Java 中的所有其他任务之前运行语音识别器,这样只有当输出包含 begin 时,程序才会继续

java - CMUSphinx 真人语音识别太慢?

javascript - electron App中如何获取appId

javascript - 子元素点击也触发父元素 - Reactjs

javascript - React可排序表不排序