-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (45 loc) · 1.45 KB
/
index.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
let voicesSelect = document.getElementById("voices");
let textArea = document.getElementById("text");
let range = document.getElementById("range");
let speak = document.getElementById("submit");
let playSection = document.getElementById("play");
//get control buttons
let play = document.getElementById("play");
let pause = document.getElementById("pause");
let stop = document.getElementById("stop");
play.addEventListener("click", (e) => {
e.preventDefault();
speechSynthesis.resume();
});
pause.addEventListener("click", (e) => {
e.preventDefault();
speechSynthesis.pause();
});
stop.addEventListener("click", (e) => {
e.preventDefault();
speechSynthesis.cancel();
});
//get all voices
let voicesArr = [];
const allVoices = () => {
voicesArr = speechSynthesis.getVoices();
voicesArr.forEach((voice, i) => {
let option = document.createElement("option");
option.value = i;
option.textContent = `${voice.name} (${voice.lang})`;
voicesSelect.appendChild(option);
});
};
speechSynthesis.addEventListener("voiceschanged", allVoices);
speak.addEventListener("click", (e) => {
e.preventDefault();
let text = textArea.value;
let utteredVoice = new SpeechSynthesisUtterance(text);
// get the selected voice
let selectedVoiceIndex = voicesSelect.value;
utteredVoice.voice = voicesArr[selectedVoiceIndex];
//voice rate
// utteredVoice.rate = range?.value;
//trigger voice to speak
speechSynthesis.speak(utteredVoice);
});