可以在播放声音的时候当检测到麦克风声音有人声的时候中断播放,识别人声为文字,完整代码如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>中文语音识别示例</title> </head> <body> <h1>中文语音识别中断示例</h1> <div> <button id="startButton">开始识别</button> <button id="stopButton">停止识别</button> </div> <div id="result"></div> <script> // 获取页面元素 const startButton = document.getElementById('startButton'); const stopButton = document.getElementById('stopButton'); const resultDiv = document.getElementById('result'); let utterance = new SpeechSynthesisUtterance('速度快放假上来看打,飞机酸辣粉几十块基辅罗斯看发蓝色,科技放开色精菲利克斯,荆防颗粒束带结发了库设计分离可,及示例快递费记录啥看,到放假了考试的解放路卡时间到了可.'); // 创建语音识别对象 const recognition = new webkitSpeechRecognition(); // 在 Chrome 中使用 webkitSpeechRecognition recognition.lang = 'zh-CN'; // 设置识别语言为中文简体 // 设置连续识别 recognition.continuous = true; recognition.interimResults = true; // 开始识别按钮点击事件 startButton.addEventListener('click', () => { startrecong(); }); function startrecong(){ recognition.start(); resultDiv.innerHTML = '正在识别...'; speechSynthesis.speak(utterance); } // 停止识别按钮点击事件 stopButton.addEventListener('click', () => { recognition.stop(); resultDiv.innerHTML = '已停止识别'; }); // 处理识别结果 recognition.onresult = (event) => { let result = ''; for (let i = event.resultIndex; i < event.results.length; i++) { if (event.results[i].isFinal) { result += event.results[i][0].transcript; } else { // result += event.results[i][0].transcript; } } if(result!=""){ console.log(result) speechSynthesis.cancel(); console.log("识别有人说话,中断声音播放") } resultDiv.innerHTML = result; }; // 处理识别错误 recognition.onerror = (event) => { resultDiv.innerHTML = '发生错误:' + event.error; }; /*recognition.onend = (event) => { resultDiv.innerHTML = '结束:' ; startrecong(); };*/ </script> </body> </html>
网友回复
python如何调用openai的api实现知识讲解类动画讲解视频的合成?
html如何直接调用openai的api实现海报可视化设计及文本描述生成可编辑海报?
f12前端调试如何找出按钮点击事件触发的那段代码进行调试?
abcjs如何将曲谱播放后导出mid和wav格式音频下载?
python如何将曲子文本生成音乐mp3或wav、mid文件
python中mp3、wav音乐如何转成mid格式?
js在HTML中如何将曲谱生成音乐在线播放并下载本地?
python如何实现在windows上通过键盘来模拟鼠标操作?
python如何给win10电脑增加文件或文件夹右键自定义菜单?
python如何将音乐mp3文件解析获取曲调数据?