我正在尝试在不单击麦克风图标的情况下启动语音输入会话。就像在窗口加载或文档加载事件时一样 - 而不是默认的单击事件。
<input type="text" id="autoStart" x-webkit-speech />
我正在使用谷歌浏览器,因为它支持大多数HTML 5功能。
在页面加载时执行此代码,您将看到一个权限对话框,要求您授予麦克风访问权限。单击允许,语音到文本识别将开始。查看语音识别 API 文档,了解可以绑定到的事件。
var interim_result, final_result;
var recognition_engine = new webkitSpeechRecognition();
recognition_engine.continuous = true;
recognition_engine.interimResults = true;
recognition_engine.lang = 'en-US';
recognition_engine.onresult = function(function(e) {
var result, i;
interim_result = '';
if (typeof e.results === 'undefined') {
recognition_engine.stop();
console.log('SPEECH RECOGNITION : stopping due to empty result.', e);
return;
}
for (i = event.resultIndex; i < event.results.length; ++i) {
result = event.results[i];
if (result.isFinal) {
final_result = result[0].transcript;
console.log('SPEECH RECOGNITION : final transcript = ' + final_result, e);
// trigger a command matching the final utterance here
} else {
interim_result += result[0].transcript;
}
}
console.log('SPEECH RECOGNITION : interim result = ' + interim_result);
};
recognition_engine.start();