-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
51 lines (39 loc) · 1.48 KB
/
script.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
const textInput = document.getElementById("text-input");
const voiceSelect = document.getElementById("voice-select");
const pitchRange = document.getElementById("pitch-range");
const rateRange = document.getElementById("rate-range");
const speakButton = document.getElementById("speak-button");
let voices = [];
function getVoicesinhtml() {
voices = window.speechSynthesis.getVoices()
voices.forEach(voice => {
const option = document.createElement("option");
option.textContent = voice.name + "(" + voice.lang + ")";
option.setAttribute("data-lang", voice.lang)
option.setAttribute("data-name", voice.name)
voiceSelect.appendChild(option);
})
}
getVoicesinhtml()
// some condition
if (typeof speechSynthesis !== "undefined" && speechSynthesis.onvoiceschanged !== "undefined") {
speechSynthesis.onvoiceschanged = getVoicesinhtml;
}
speakButton.addEventListener("click", () => {
const speech = new SpeechSynthesisUtterance(textInput.value);
speech.pitch = pitchRange.value;
speech.rate = rateRange.value;
// console.log(voices)
// console.log(voices.find(voice => {
// return (
// voice.name == voiceSelect.selectedOptions[0].getAttribute("data-name")
// );
// }))
speech.voice = voices.find(voice => {
return (
voice.name == voiceSelect.selectedOptions[0].getAttribute("data-name")
);
})
// console.log(speech)
window.speechSynthesis.speak(speech);
})