-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
77 lines (68 loc) · 2.49 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
let utterance = new SpeechSynthesisUtterance(); // create SpeechSynthesisUtterance object
let isPaused = false;
let voices = [];
let voiceSelect = document.querySelector("select")
const textarea = document.querySelector("textarea");
//local the saved text from local storage
const savedText = localStorage.getItem("savedText");
if(savedText){
textarea.value = savedText;
}
speechSynthesis.onvoiceschanged = ()=>{
voices = speechSynthesis.getVoices();
speechSynthesis.voice = voices[0]; //default choice for the voice (first voice)
voices.forEach((voice, i)=>(voiceSelect.options[i] = new Option(voice.name, i) ));
}
//add change event to the dropdown select
voiceSelect.addEventListener("change", ()=>{
utterance.voice = voices[voiceSelect.value];
})
//button click event to convert text to speech or pause/resume
const button = document.querySelector("button");
const playIcon = document.querySelector(".play-icon");
const pauseIcon = document.querySelector(".pause-icon");
button.addEventListener("click", ()=>{
if (!isPaused){
if (speechSynthesis.speaking){
//pause the speech
speechSynthesis.pause();
isPaused = true;
playIcon.style.display = "inline";
pauseIcon.style.display = "none";
}else {
//set the text
utterance.text = textarea.value;
//set the rate and pitch
utterance.rate = parseFloat(document.getElementById("rate").value);
utterance.pitch = parseFloat(document.getElementById("pitch").value);
speechSynthesis.speak(utterance);
playIcon.style.display = "none";
pauseIcon.style.display = "inline";
}
}else {
//resume the speech
speechSynthesis.resume();
isPaused = false;
playIcon.style.display = "none";
pauseIcon.style.display = "inline";
}
});
//save the text in the textarea to local storage
textarea.addEventListener("input", ()=>{
localStorage.setItem("savedText", textarea.value);
})
// document.querySelector("button").addEventListener("click", ()=>{
// if(!isPaused){
// //set the text
// utterance.text = document.querySelector("textarea").value;
// speechSynthesis.speak(utterance);
// }else{
// if(speechSynthesis.paused){
// speechSynthesis.resume();
// isPaused = false;
// }else {
// speechSynthesis.pause();
// isPaused = true;
// }
// }
// });