Skip to content

Instantly share code, notes, and snippets.

@t-kashima
Created May 26, 2013 16:03
Show Gist options
  • Save t-kashima/5653194 to your computer and use it in GitHub Desktop.
Save t-kashima/5653194 to your computer and use it in GitHub Desktop.
Chrome is Listening to your voice.
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>web speech api</title>
</head>
<body>
<input type="button" value="talk" id="speech_button">
<script type="text/javascript" src="./speech.js"></script>
</body>
</html>
var elementSpeechButton = document.querySelector('#speech_button');
var trim = function(str) {
return str.replace(/ /g, '');
}
var recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.onstart = function(event) {
console.log('start speech');
}
recognition.onerror = function(event) {
console.log('error');
}
recognition.onend = function(event) {
console.log('end speech');
}
recognition.onresult = function(event) {
var length = event.results.length;
if (length > 0) {
var text = trim(event.results[length - 1][0].transcript);
console.log('text:' + text);
}
}
elementSpeechButton.addEventListener('click', function() {
recognition.start();
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment