-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (42 loc) · 1.48 KB
/
index.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
var speechMsgInput = document.getElementById('speech-msg');
// Get the voice select element.
var voiceSelect = document.getElementById('voice');
function loadVoices() {
// Fetch the available voices.
var voices = speechSynthesis.getVoices();
// Loop through each of the voices.
voices.forEach(function(voice, i) {
// Create a new option element.
var option = document.createElement('option');
// Set the options value and text.
option.value = voice.name;
option.innerHTML = voice.name;
// Add the option to the voice selector.
voiceSelect.appendChild(option);
});
}
// Execute loadVoices.
loadVoices();
// Chrome loads voices asynchronously.
window.speechSynthesis.onvoiceschanged = function(e) {
loadVoices();
};
// Create a new utterance for the specified text and add it to
// the queue.
function speak(text) {
// Create a new instance of SpeechSynthesisUtterance.
var msg = new SpeechSynthesisUtterance();
// Set the text.
msg.text = text;
if (voiceSelect.value) {
msg.voice = speechSynthesis.getVoices()[113];
}
// Queue this utterance.
window.speechSynthesis.speak(msg);
}
// Set up an event listener for when the 'speak' button is clicked.
button.addEventListener('click', function(e) {
if (speechMsgInput.value.length > 0) {
speak(speechMsgInput.value);
}
});