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