1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <h1>Web Speech API</h1> <button id="startButton">Start</button> <button id="stopButton">Stop</button> <p> Result: <span id="result"></span> </p> <script> if ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window) { const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; const recognition = new SpeechRecognition(); recognition.lang = 'zh-TW'; recognition.continuous = true; recognition.interimResults = true; recognition.onresult = (event) => { let transcript = ''; for (let i = 0; i < event.results.length; i++) { transcript += event.results[i][0].transcript; } document.getElementById('result').innerText = transcript; }; document.getElementById('startButton').onclick = () => { recognition.start(); document.getElementById('result').innerText = 'Listening...'; }; document.getElementById('stopButton').onclick = () => { recognition.stop(); }; } else { alert('Web Speech API is not supported.'); } </script> </body> </html>
|