-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
35 lines (27 loc) · 1.1 KB
/
Copy pathscript.js
File metadata and controls
35 lines (27 loc) · 1.1 KB
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
const textInput = document.getElementById('text-input');
const voiceSelect = document.getElementById('voice-select');
const speakButton = document.getElementById('speak-btn');
let voices = [];
const populateVoiceList = () => {
voices = window.speechSynthesis.getVoices();
voiceSelect.innerHTML = '';
voices.forEach((voice) => {
const option = document.createElement('option');
option.value = voice.name;
option.textContent = `${voice.name} (${voice.lang})`;
voiceSelect.appendChild(option);
});
};
const speak = () => {
const text = textInput.value;
const utterance = new SpeechSynthesisUtterance(text);
const selectedVoice = voiceSelect.value;
// Set the voice
utterance.voice = voices.find((voice) => voice.name === selectedVoice);
// Speak the text
window.speechSynthesis.speak(utterance);
};
// Populate the voice list on load
window.speechSynthesis.onvoiceschanged = populateVoiceList;
// Event listeners
speakButton.addEventListener('click', speak);