-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.html
62 lines (50 loc) · 1.65 KB
/
example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<input type="button" id="toggle" value="Stop"><br>
<textarea style="height: 300px; width: 500px;" id="output"></textarea>
<script src="speech.js"></script>
<script>
/** --- **/
var output = document.getElementById("output");
var button = document.getElementById("toggle");
button.onclick = function() {
if(this.value == "Stop") {
sr.stop();
this.value = "Start";
} else {
sr.start();
this.value = "Stop";
}
}
/** --- **/
var sr = new SpeechRecognition();
/** Events **/
sr.on("starting", function() { console.log("[SpeechRecognition]", "Starting..."); });
sr.on("started", function() { button.value = "Stop"; console.log("[SpeechRecognition]", "Started."); });
sr.on("stopping", function() { console.log("[SpeechRecognition]", "Stopping..."); });
sr.on("stopped", function() { button.value = "Start"; console.log("[SpeechRecognition]", "Stopped."); });
sr.on("optionschanged", function() { console.log("[SpeechRecognition]", "Options changed."); });
/** Events **/
/** Set options **/
sr.set("language", "en-GB");
sr.set({
language: "hu-HU",
continuous: true,
interimResults: false
});
/** Error & Result Events **/
sr.on("error", function(e) {
console.log("[SpeechRecognition]", "Error:", e);
});
sr.on("result", function(evt) {
console.log("[SpeechRecognition]", "Result:", evt);
for (var i = evt.resultIndex; i < evt.results.length; ++i) {
if (evt.results[i].isFinal) {
output.value += evt.results[i][0].transcript +" | ";
} else {
//evt.results[i][0].transcript; <-- Not fully recognized (alias: interim script)!
}
}
});
/** Start & Stop **/
sr.start();
//sr.stop();
</script>