webkitSpeechRecognition
Research Mozilla's speech recognition web API
by humanzing
HTML
<!DOCTYPE HTML>
<html lang = "ru-RU">
<head>
<title>speechrecognitiondemo.html</title>
<meta charset = "UTF-8" />
</head>
<body>
<h1>Web Speech API Demo</h1>
<textarea id = "myTextArea" rows = "30" cols = "80"></textarea>
</body>
</html>
JavaScript
// https://developers.google.com/web/updates/2013/01/Voice-Driven-Web-Apps-Introduction-to-the-Web-Speech-API
speechSynthesis.speak(new SpeechSynthesisUtterance('Начинайте диктовку текста:'));
function startRecognizer() {
if ('webkitSpeechRecognition' in window) {
var recognition = new webkitSpeechRecognition();
// Option, false is meaning that when the user stops talking, speech recognition will end
recognition.continuous = true;
// Выбираем русский язык в качестве целевого
recognition.lang = 'ru-RU';
// Если хотим чтобы распознавание началось ещё до того, как пользователь закончит говорить
recognition.interimResults = true;
recognition.onresult = function (event) {
var result = event.results[event.resultIndex];
var textarea = document.getElementById('myTextArea');
if (result.isFinal) {
textarea.value = 'Вы продикотвали: ' + result[0].transcript;
//alert('Вы продикотвали: ' + result[0].transcript);
} else {
textarea.value = 'Промежуточный результат: ' + result[0].transcript;
//console.log('Промежуточный результат: ', result[0].transcript);
}
};
recognition.onstart = function() {
/*var textarea = document.getElementById('myTextArea');
textarea.value = "Hello!\nHow are you";*/
}
recognition.onend = function() {
console.log('Распознавание завершено.');
speechSynthesis.speak(new SpeechSynthesisUtterance('Распознавание завершено.'));
};
speechSynthesis.speak(new SpeechSynthesisUtterance('Начинайте диктовку текста:'));
recognition.start();
} else {
alert('webkitSpeechRecognition не поддерживается вашим браузером, используйте Google Chrome или Mozilla Firefox');
}
}
startRecognizer();