每日总结 5.1
今日为劳动节今天学习了HTML的语音识别文字。
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Ai语音转文字</title> <style> * { margin: 0; padding: 0; } body { display: flex; justify-content: space-around; align-items: center; } .startBtn, .endBtn { display: block; width: 150px; height: 120px; border: 2px solid; margin: 10px; } </style> </head> <body> <button onclick="start()" class="startBtn">开始说话</button> <br /> <button onclick="end()" class="endBtn">结束并转换普通话</button> </body> </html> <script> var recognition = new webkitSpeechRecognition() || new SpeechRecognition(); recognition.continuous = true; //识别到声音就关闭,还是一直识别 console.log(recognition) recognition.lang = 'cmn-Hans-CN'; //定义普通话 (中国大陆) function start() { console.log('start') // 开启 recognition.continuous = true; recognition.start(); } function end() { console.log('end') // 停止 recognition.stop(); recognition.continuous = false; } // 当调用recognition的stop的时候会触发此对象的onresult事件,然后我们在这里获取我们的转换结果。 recognition.onresult = function (event) { alert(event.results[0][0].transcript); // console.log(event) console.log(event.results[0][0].transcript) } recognition.onsoundstart = function (e) { console.log("开始收听了"); console.log(e); } recognition.onspeechstart = (e) => { console.log("开始讲话了"); console.log(e); } recognition.onspeechend = (e) => { console.log("讲话完毕"); console.log(e); } recognition.onerror = (e) => { console.log("发生异常"); console.log(e); } </script>