luistavares

JavaScript - Reconhecimento de voz com a Web Speech API

Jan 26th, 2023 (edited)
2,568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.91 KB | Source Code | 0 0
  1. <!DOCTYPE html>
  2. <html lang="pt-br">
  3. <head>
  4.     <meta charset="utf-8">
  5.     <title>Reconhecimento de Voz</title>
  6.     <style>
  7.         .words {            
  8.             height:400px;
  9.             width: 700px;
  10.             border: 1px solid black;
  11.             overflow-y: scroll;
  12.         }
  13.         .words span {
  14.             font-size: 22px;
  15.         }
  16.         .button_speech {
  17.             font-size: 22px;
  18.         }
  19.     </style>
  20. </head>
  21. <body>
  22.     <h1>Reconhecimento de Voz com a Web Speech API</h1>        
  23.     <div class="words"></div><br>
  24.     <button id="btn_speech" onclick='doStartStopCheck()'>
  25.             Transcrever Áudio
  26.     </button>
  27.     <script>
  28.         var ongoing = false;
  29.         var recognition = null;
  30.  
  31.         function verificaStatus(){
  32.             if (ongoing == true){
  33.                 recognition.start();
  34.             }
  35.         }
  36.  
  37.         function init(){
  38.             window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
  39.             recognition = new SpeechRecognition();
  40.             recognition.interimResults = true;
  41.             recognition.lang = 'pt-BR';
  42.  
  43.             var p = document.createElement('span');
  44.             const words = document.querySelector('.words');
  45.             words.appendChild(p);
  46.  
  47.             recognition.addEventListener('result', e => {
  48.                 const transcript = Array.from(e.results)
  49.                 .map(result => result[0])
  50.                 .map(result => result.transcript)
  51.                 .join('');
  52.            
  53.                 p.textContent = transcript + ", ";
  54.                 if (e.results[0].isFinal) {
  55.                     p = document.createElement('span');
  56.                     words.appendChild(p);      
  57.                 }
  58.                 });
  59.             recognition.addEventListener('end', verificaStatus);
  60.             recognition.start();
  61.         }
  62.  
  63.         function doStartStopCheck(){
  64.             if(ongoing == true){ // se tiver rodando, vai interromper
  65.                 ongoing = false;
  66.                 recognition.stop();    
  67.                 document.getElementById('btn_speech').innerHTML = "Transcrever Áudio";
  68.             }
  69.             else if (recognition) { // se tiver instância SpeechRecognition, apenas reinicia
  70.                 ongoing = true;
  71.                 recognition.start();       
  72.                 document.getElementById('btn_speech').innerHTML = "Interromper";
  73.             }
  74.             else { // se ainda não criou instância, chama a função para inicialização
  75.                 console.log("init");
  76.                 ongoing = true;
  77.                 init();    
  78.                 document.getElementById('btn_speech').innerHTML = "Interromper";
  79.             }
  80.         }
  81.  
  82.         function rolaScroll(){
  83.             const w = document.querySelector('.words');
  84.             w.scrollTop = w.scrollHeight;
  85.         }
  86.  
  87.         setInterval(rolaScroll, 1000);
  88.  
  89.     </script>
  90. </body>
  91. </html>
  92.  
Advertisement
Add Comment
Please, Sign In to add comment