Advertisement
sEi_

SimpleSpeechRecognition

Jul 5th, 2023 (edited)
1,050
2
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Speech Recognition</title>
  5. </head>
  6. <body>
  7.     <h1>Speech Recognition</h1>
  8.     <button onclick="startListening()">Start Listening</button>
  9.     <button onclick="stopListening()">Stop Listening</button>
  10.     <p id="result"></p>
  11.  
  12.     <script>
  13.         let recognizer;
  14.         let audioChunks = [];
  15.  
  16.         // Function to start speech recognition
  17.         function startListening() {
  18.             recognizer = new window.webkitSpeechRecognition();
  19.             recognizer.continuous = true;
  20.             recognizer.interimResults = true;
  21.             recognizer.lang = "en-US";
  22.             recognizer.onresult = function(event) {
  23.                 let result = event.results[event.results.length - 1];
  24.                 if (result.isFinal) {
  25.                     let text = result[0].transcript;
  26.                     console.log("You said: " + text);
  27.                     document.getElementById("result").textContent = text;
  28.                 }
  29.             };
  30.             recognizer.onend = function() {
  31.                 console.log("Speech recognition ended.");
  32.             };
  33.             recognizer.start();
  34.         }
  35.  
  36.         // Function to stop speech recognition
  37.         function stopListening() {
  38.             recognizer.stop();
  39.         }
  40.     </script>
  41. </body>
  42. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement