-
Notifications
You must be signed in to change notification settings - Fork 0
/
recognition.js
41 lines (34 loc) · 1.26 KB
/
recognition.js
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
// test if this browser has sppech recognition api
const speakBtn = document.querySelector('#speak-btn');
const recordedText = document.querySelector('#recorded-text');
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
if(!SpeechRecognition){
alert('Sorry, your browser does not support this feature!')
// return
} else {
const recognition = new SpeechRecognition();
recognition.interimResults = true;
// Recognition start event handler
// recognition.onstart = () => {
// console.log('Voice recognition started. Try speaking into the microphone.');
// }
recognition.addEventListener('result', e => {
// console.log(e);
const transcript = Array.from(e.results)
.map(result => result[0])
.map(result => result.transcript)[0];
// .join('');
recordedText.textContent = transcript;
});
recognition.addEventListener('end', () => {
console.log('Speech recognition has stopped.');
if(recordedText.textContent.includes('joke') || recordedText.textContent.includes('笑話')) {
getJoke();
}
});
speakBtn.addEventListener('click', () => {
// start recognition
console.log('Voice recognition started. Try speaking into the microphone.');
recognition.start();
});
}