-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
35 lines (31 loc) · 1.14 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
// Ensure the Web Speech API is supported
if ('speechSynthesis' in window) {
const speakButton = document.getElementById('speak');
const stopButton = document.getElementById('stop');
const textInput = document.getElementById('text');
const speedInput = document.getElementById('speed');
let utterance;
speakButton.addEventListener('click', () => {
const text = textInput.value.trim();
if (text !== '') {
// Create a new instance of SpeechSynthesisUtterance
utterance = new SpeechSynthesisUtterance(text);
// Set the speech speed
utterance.rate = speedInput.value;
// Speak the text
window.speechSynthesis.speak(utterance);
} else {
alert('Please enter some text to speak.');
}
});
stopButton.addEventListener('click', () => {
if (window.speechSynthesis.speaking) {
// Cancel the speech
window.speechSynthesis.cancel();
} else {
alert('Not speaking right now.');
}
});
} else {
alert('Sorry, your browser does not support the Web Speech API.');
}