Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="pt-br">
- <head>
- <meta charset="utf-8">
- <title>Reconhecimento de Voz</title>
- <style>
- .words {
- height:400px;
- width: 700px;
- border: 1px solid black;
- overflow-y: scroll;
- }
- .words span {
- font-size: 22px;
- }
- .button_speech {
- font-size: 22px;
- }
- </style>
- </head>
- <body>
- <h1>Reconhecimento de Voz com a Web Speech API</h1>
- <div class="words"></div><br>
- <button id="btn_speech" onclick='doStartStopCheck()'>
- Transcrever Áudio
- </button>
- <script>
- var ongoing = false;
- var recognition = null;
- function verificaStatus(){
- if (ongoing == true){
- recognition.start();
- }
- }
- function init(){
- window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
- recognition = new SpeechRecognition();
- recognition.interimResults = true;
- recognition.lang = 'pt-BR';
- var p = document.createElement('span');
- const words = document.querySelector('.words');
- words.appendChild(p);
- recognition.addEventListener('result', e => {
- const transcript = Array.from(e.results)
- .map(result => result[0])
- .map(result => result.transcript)
- .join('');
- p.textContent = transcript + ", ";
- if (e.results[0].isFinal) {
- p = document.createElement('span');
- words.appendChild(p);
- }
- });
- recognition.addEventListener('end', verificaStatus);
- recognition.start();
- }
- function doStartStopCheck(){
- if(ongoing == true){ // se tiver rodando, vai interromper
- ongoing = false;
- recognition.stop();
- document.getElementById('btn_speech').innerHTML = "Transcrever Áudio";
- }
- else if (recognition) { // se tiver instância SpeechRecognition, apenas reinicia
- ongoing = true;
- recognition.start();
- document.getElementById('btn_speech').innerHTML = "Interromper";
- }
- else { // se ainda não criou instância, chama a função para inicialização
- console.log("init");
- ongoing = true;
- init();
- document.getElementById('btn_speech').innerHTML = "Interromper";
- }
- }
- function rolaScroll(){
- const w = document.querySelector('.words');
- w.scrollTop = w.scrollHeight;
- }
- setInterval(rolaScroll, 1000);
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment