-
-
Save robsonamendonca/797a22c6b53139f9c53acd4624e71c5b to your computer and use it in GitHub Desktop.
Simple Voice Recognition in JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Simple Command Voice</title> | |
</head> | |
<body> | |
<p id="output"></p> | |
<button id="start">Click and say something!</button> | |
<script> | |
(() => { | |
const startBtn = document.querySelector('#start'); | |
const output = document.querySelector('#output'); | |
function start() { | |
const recognition = new webkitSpeechRecognition(); | |
recognition.continuous = true; | |
recognition.interimResults = true; | |
recognition.lang = "pt-BR"; | |
recognition.continuous = true; | |
recognition.start(); | |
// Este evento ocorre toda vez que falar algo no microfone | |
recognition.onresult = function(event) { | |
for (let i = event.resultIndex; i < event.results.length; i++) { | |
if (event.results[i].isFinal) { | |
// É aqui que será capturado uma palavra ou frase dita no microfone | |
const content = event.results[i][0].transcript.trim(); | |
output.textContent = content; | |
} | |
} | |
}; | |
}; | |
// Tudo vai começar ao clicar no botão para iniciar a função start() acima :) | |
startBtn.addEventListener('click', () => start()); | |
})(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment