-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
77 lines (62 loc) · 2.07 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
const speech = new SpeechSynthesisUtterance();
let voices = [];
let voiceSelect = document.querySelector("select");
window.speechSynthesis.onvoiceschanged = () => {
voices = window.speechSynthesis.getVoices();
speech.voice = voices[0];
voices.forEach((voice, i) => (voiceSelect.options[i] = new Option(voice.name, i)));
};
voiceSelect.addEventListener("change", () => {
speech.voice = voices[voiceSelect.value];
})
//document.querySelector("button").addEventListener("click", () => {
// speech.text = document.querySelector("textarea").value;
// window.speechSynthesis.speak(speech);
//})
const listenBtn = document.getElementById('listen');
const pauseBtn = document.getElementById('pause');
const resumeBtn = document.getElementById('resume');
const stopBtn = document.getElementById('stop');
let isPaused = false;
listenBtn.addEventListener('click', function() {
speech.text = document.querySelector("textarea").value;
speechSynthesis.speak(speech);
listenBtn.style.display = 'none';
pauseBtn.style.display = 'inline';
stopBtn.style.display = 'inline';
});
pauseBtn.addEventListener('click', function() {
speechSynthesis.pause();
isPaused = true;
pauseBtn.style.display = 'none';
resumeBtn.style.display = 'inline';
});
resumeBtn.addEventListener('click', function() {
speechSynthesis.resume();
isPaused = false;
resumeBtn.style.display = 'none';
pauseBtn.style.display = 'inline';
});
stopBtn.addEventListener('click', function() {
speechSynthesis.cancel();
resetButtons();
});
speech.onend = function() {
if (!isPaused) resetButtons();
};
speech.onend = function(event) {
resetButtons();
};
function resetButtons() {
listenBtn.style.display = 'inline';
pauseBtn.style.display = 'none';
resumeBtn.style.display = 'none';
stopBtn.style.display = 'none';
isPaused = false; // Ensure isPaused is reset correctly
}
function resetButtons() {
listenBtn.style.display = 'inline';
pauseBtn.style.display = 'none';
resumeBtn.style.display = 'none';
stopBtn.style.display = 'none';
}