Skip to content

Commit 8041abe

Browse files
committed
20
1 parent 6a55353 commit 8041abe

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

20 - Speech Detection/index-START.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,29 @@
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+
28+
p.textContent = transcript;
29+
if (e.results[0].isFinal) {
30+
p = document.createElement('p');
31+
words.appendChild(p);
32+
}
33+
});
34+
35+
recognition.addEventListener('end', recognition.start);
36+
recognition.start();
37+
1538

1639
</script>
1740

0 commit comments

Comments
 (0)