Created
May 26, 2013 16:03
-
-
Save t-kashima/5653194 to your computer and use it in GitHub Desktop.
Chrome is Listening to your voice.
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 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> | |
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
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