-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
53 lines (43 loc) · 1.26 KB
/
app.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
42
43
44
45
46
47
48
49
50
51
52
53
const talkBtn = document.querySelector("#talk");
const content = document.querySelector("#content");
const speakerCheckbox = document.querySelector("#speakerCheckbox");
let soundOutput = true;
speakerCheckbox.addEventListener("change", function() {
if (this.checked) {
soundOutput = true;
} else {
soundOutput = false;
}
});
const SpeechRecognition =
window.SpeechRecognition || window.webkitSpeechRecognition;
// ##
const recognition = new SpeechRecognition();
recognition.onstart = () => {
// console.log("Voice is activated");
talkBtn.style.backgroundColor = "#f44336";
talkBtn.style.color = "white";
talkBtn.textContent = "LISTINING..";
};
recognition.onend = () => {
talkBtn.style.backgroundColor = "white";
talkBtn.style.color = "black";
talkBtn.textContent = "START";
};
recognition.onresult = event => {
const current = event.resultIndex;
const transcript = event.results[current][0].transcript;
content.textContent = "You Said : " + transcript;
if (soundOutput) {
readOutLoud("You Said : " + transcript);
}
};
//
talkBtn.addEventListener("click", () => {
recognition.start();
});
function readOutLoud(msg) {
const speech = new SpeechSynthesisUtterance();
speech.text = msg;
window.speechSynthesis.speak(speech);
}