You can create a dictation system for your user with the original webkitSpeechRecognition
, however it's not a quick task to do. Instead you can use the artyom library to accomplish this.
Note
A dictation object can't be active when artyom is active, is recommendable to deactivate artyom with artyom.fatality()
before start a newDictation object to prevent errors, you can activate artyom again when the event onEnd of the dictation object is triggered.
The newDictation
method expects a single argument, an object with the described properties:
Property name | Type | Description |
continuous |
Boolean | If you have a https connection, this property can be set to true and the dictation object will NEVER STOP unless you execute stop method. Without a https connection, the dictation object will stop after a determinated time. |
onResult |
Function | You can manipulate the output of the dictation in this property, see examples down. |
onStart |
Function | Do something when the dictation event is started |
onEnd |
Function | Do something when the dictation is stopped |
The usage logic is simple, create a dictation object with the newDictation method, and store it in a variable. The returned object will have 2 properties start
and stop
.
Example
var settings = {
continuous:true, // Don't stop never because i have https connection
onResult:function(text){
// Show the Recognized text in the console
console.log("Recognized text: ", text);
},
onStart:function(){
console.log("Dictation started by the user");
},
onEnd:function(){
alert("Dictation stopped by the user");
}
};
var UserDictation = artyom.newDictation(settings);
// Link the events to buttons in the document
$("#button-start").click(function(){
// Start the Dictation
UserDictation.start();
});
$("#button-stop").click(function(){
// Stop the Dictation
UserDictation.stop();
});