Hello everyone,
HTML5 , javascript provides Speech Recognition API,with the help of the API the user can talk with the browser without the using the keyboard. Html 5 provides voice search option like in google IE and chrome.It uses x-webkit-speech to make a search box voice capable. Now we require javascript for speech recognition. We use CSS to place the microphone image inside the input box, the form code containing the input button and the JavaScript that does all the heavy work.when the user clicks the icon the javascript allow and check for the browser support,if the browser supports the API then the user voice search will activate.
HTML and JavaScript:-
<div>
<a href="#" id="start_button" onclick="startDictation(event)">Speak</a>
</div>
<div id="results">
<span id="final_span" class="final"></span>
<span id="interim_span" class="interim"></span>
</div>
<script type="text/javascript">
var final_transcript = '';
var recognizing = false;
if ('webkitSpeechRecognition' in window) {
var recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.onstart = function() {
recognizing = true;
};
recognition.onerror = function(event) {
console.log(event.error);
};
recognition.onend = function() {
recognizing = false;
};
recognition.onresult = function(event) {
var interim_transcript = '';
for (var i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
final_transcript += event.results[i][0].transcript;
} else {
interim_transcript += event.results[i][0].transcript;
}
}
final_transcript = capitalize(final_transcript);
final_span.innerHTML = linebreak(final_transcript);
interim_span.innerHTML = linebreak(interim_transcript);
};
}
var two_line = /\n\n/g;
var one_line = /\n/g;
function linebreak(s) {
return s.replace(two_line, '<p></p>').replace(one_line, '<br>');
}
function capitalize(s) {
return s.replace(s.substr(0,1), function(m) { return m.toUpperCase(); });
}
function startDictation(event) {
if (recognizing) {
recognition.stop();
return;
}
final_transcript = '';
recognition.lang = 'en-US';
recognition.start();
final_span.innerHTML = '';
interim_span.innerHTML = '';
}
</script>
0 Comment(s)