实时语音识别功能 JavaScript 代码实现
// 实时语音识别功能
// 首先定义一个函数,该函数负责处理语音识别
function speechRecognition() {
// 获取用户的输入
var input = document.getElementById('speechInput').value;
// 使用 JavaScript 的 Web Speech API 进行语音识别
var recognition = new webkitSpeechRecognition();
// 设置语言
recognition.lang = 'en-us';
// 开始识别
recognition.start();
// 监听 result 事件
recognition.addEventListener('result', function(event) {
var input = event.results[0][0].transcript;
// 将识别的输入赋值给 input 元素
document.getElementById('speechInput').value = input;
});
}
// 当输入框获取焦点时,调用 speechRecognition 函数
document.getElementById('speechInput').addEventListener('focus', speechRecognition);
原文地址: https://www.cveoy.top/t/topic/ljX6 著作权归作者所有。请勿转载和采集!