Skip to content

Commit 609c1b0

Browse files
committed
Completed lesson 20
SpeechRecognition. Is awesome. Kinda sux. More to come.
1 parent 3d52215 commit 609c1b0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

20 - Speech Detection/index-START.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@
1212
<script>
1313
window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
1414

15+
const recognition = new SpeechRecognition();
16+
recognition.interimResults = true;
17+
18+
let p = document.createElement('p');
19+
const words = document.querySelector('.words');
20+
words.appendChild(p);
21+
22+
recognition.addEventListener('result', e => {
23+
const transcript = Array.from(e.results)
24+
.map(result => result[0])
25+
.map(result => result.transcript)
26+
.join('');
27+
p.textContent = transcript;
28+
if (e.results[0].isFinal) {
29+
p = document.createElement('p');
30+
words.appendChild(p);
31+
}
32+
});
33+
34+
recognition.addEventListener('end', recognition.start);
35+
recognition.start();
36+
1537

1638
</script>
1739

0 commit comments

Comments
 (0)