Skip to content

Instantly share code, notes, and snippets.

@robsonamendonca
Forked from caio-ribeiro-pereira/index.html
Created February 2, 2019 13:50
Show Gist options
  • Save robsonamendonca/797a22c6b53139f9c53acd4624e71c5b to your computer and use it in GitHub Desktop.
Save robsonamendonca/797a22c6b53139f9c53acd4624e71c5b to your computer and use it in GitHub Desktop.
Simple Voice Recognition in JS
<!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