Skip to content

Instantly share code, notes, and snippets.

@caio-ribeiro-pereira
Last active February 26, 2020 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save caio-ribeiro-pereira/6afd46acff0580320f1800e0588e9c1c to your computer and use it in GitHub Desktop.
Save caio-ribeiro-pereira/6afd46acff0580320f1800e0588e9c1c 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.interimResults = true;
recognition.lang = "en-US";
recognition.continuous = true;
recognition.start();
// This event happens when you talk in the microphone
recognition.onresult = function(event) {
for (let i = event.resultIndex; i < event.results.length; i++) {
if (event.results[i].isFinal) {
// Here you can get the string of what you told
const content = event.results[i][0].transcript.trim();
output.textContent = content;
}
}
};
};
startBtn.addEventListener('click', () => start());
})();
</script>
</body>
</html>
@edysegura
Copy link

edysegura commented Dec 18, 2018

Is there a reason why this instruction is repeated twice at lines 15 and 18??

recognition.continuous = true;

@caio-ribeiro-pereira
Copy link
Author

@edysegura there is no reason, it's just a bug mine! hehehe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment