luistavares

JavaScript - Como sintetizar voz usando a web speech API

Oct 22nd, 2022
1,975
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.06 KB | Software | 0 0
  1. <!DOCTYPE html>
  2. <html lang="pt-br">
  3.   <head>
  4.     <meta charset="utf-8" />
  5.     <title>Web Speech API - Sintetizador</title>
  6.   </head>
  7.   <body>
  8.     <h1>Web Speech API - Sintetizador</h1>
  9.     <p>Digite o texto no campo de entrada e clique no botão.</p>
  10.     <input id="txt" type="text" size="50"/>      
  11.     <button id="play" type="button">Play</button>    
  12.     <script>
  13.       const synth = window.speechSynthesis;
  14.  
  15.       function talk() {
  16.         let t = document.getElementById('txt').value;
  17.         let voices = synth.getVoices();
  18.         if (voices.length !== 0) {
  19.           console.log("talk");            
  20.           let msg = new SpeechSynthesisUtterance();
  21.           msg.voice = voices[0];      // seleciono a primeira voz
  22.           msg.rate = 1;               // velocidade
  23.           msg.pitch = 1;              // tom
  24.           msg.text = t;  // pegando a msg do campo
  25.           msg.lang = "pt-BR";
  26.           synth.speak(msg);  
  27.         }
  28.       }
  29.  
  30.       document.getElementById("play").onclick = talk; // evento
  31.     </script>
  32.   </body>
  33. </html>
  34.  
Advertisement
Add Comment
Please, Sign In to add comment