我正在使用浏览器技术 SpeechRecognition/webkitSpeechRecognition 在浏览器中进行语音识别。我需要麦克风在打开页面后始终听取命令,在用户说出所需短语后,我会根据命令更改视频的状态(例如,播放/暂停)。
在Chrome浏览器的桌面上,一切正常,打开页面后麦克风开始监听(rec.start(),浏览器在页面上显示允许使用麦克风的通知,用户同意) ) 如果用户什么也没说,那么浏览器会在 7-10 秒的静音后停止录制,但是在停止麦克风后我重新启动它并且不会引起任何问题。
一个在 Chrome 桌面上运行良好的简化示例:
let rec = null;
let isRecording = false;
function startRecording() {
if (rec && !isRecording) rec.start();
}
function stopRecording() {
if (rec && isRecording) rec.stop();
}
// Сhecking the technology support in the browser
try {
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
rec = new SpeechRecognition();
} catch(e) {
console.log(e);
}
// If the technology is supported, then we set up the recording
if (rec) {
rec.continuous = true;
rec.lang = 'en-US';
rec.interimResults = false;
// Event after receiving voice transcription as text
rec.onresult = function(e) {
// Сode that compares what the user said with existing commands,
// after which some action takes place with the video on the page
}
// The event is triggered after recording has started
// rec.start();
rec.onstart = function(e) {
isRecording = true;
}
// Event after recording stopped
// rec.stop(); or after an error occurs
rec.onend = function(e) {
isRecording = false;
startRecording(); // Restart recording
}
// Event that occurs when audio recording fails
rec.onerror = function(e) {
console.log('Speech recognition error detected: ' + e.error);
}
// Start recording after page load
startRecording();
}
在safari浏览器中(iOS),用户第一次说出这句话后,脚本处理了onresult事件,该事件将不再触发,但录音不会返回错误(onerror 事件)且不停止(onend 事件),在地址栏中继续显示红色麦克风图标。为什么会发生这种情况?
我在处理完第一个短语后尝试重新开始录音,它开始工作,但不幸的是它不是很稳定,因为有时麦克风会因为其他原因停止处理命令,我也无法使用内置事件进行跟踪录音。我只需要提出某种定期重启,例如通过计时器,但我认为这不是最佳选择。
有助于在 Safari 中聆听多个短语的更改:
// Event after receiving voice transcription as text
rec.onresult = function(e) {
// Сode that compares what the user said with existing commands,
// after which some action takes place with the video on the page
// Restart recording;
restartRecording();
}
我还注意到,如果我不使用带麦克风的耳机,那么浏览器或设备在录制时会将扬声器中的视频声音降至最低,这是应该的吗?
UPD
经过多次测试,我意识到这不是唯一可用的识别问题,而是视频和SpeechRecognition之间的冲突,如果启动SpeechRecognition并且此时视频发生播放/暂停,则识别崩溃并停止提供反馈,尽管错误事件或停止不起作用,如果之后您重新启动录制,那么一切都会重新开始。最难的是找到合适的重启时机,现在我设置了一个3.5-5秒的定时器延迟重启,因为提前重启对恢复语音识别没有帮助,但这是一个很长的时间。>