gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:24:24 33 4
gpt4 key购买 nike

我想让我的语音识别 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 回调中添加一个检查以检查开始 recognition 是否再次。有点像

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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com