Skip to content

Commit 322775e

Browse files
committed
Update 20 with logic from tutorial
1 parent b418aa0 commit 322775e

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

20 - Speech Detection/index-START.html

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,30 @@
1212
<script>
1313
window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
1414

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

1640
</script>
1741

@@ -40,7 +64,7 @@
4064
position: relative;
4165
line-height: 3rem;
4266
}
43-
67+
4468
p {
4569
margin: 0 0 3rem;
4670
}

0 commit comments

Comments
 (0)