Last active
February 26, 2020 17:01
-
-
Save caio-ribeiro-pereira/6afd46acff0580320f1800e0588e9c1c to your computer and use it in GitHub Desktop.
Simple Voice Recognition in JS
This file contains hidden or 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.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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@edysegura there is no reason, it's just a bug mine! hehehe