Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 52bc0ac

Browse files
committed
Completed lesson 23
The voices in my <head>
1 parent cb9b981 commit 52bc0ac

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

23 - Speech Synthesis/index-START.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,37 @@ <h1>The Voiceinator 5000</h1>
3535
const options = document.querySelectorAll('[type="range"], [name="text"]');
3636
const speakButton = document.querySelector('#speak');
3737
const stopButton = document.querySelector('#stop');
38+
39+
msg.text = document.querySelector('[name="text"]').value;
40+
41+
function populateVoices() {
42+
voices = this.getVoices();
43+
const voiceOptions = voices
44+
.filter(voice => voice.lang.includes('en'))
45+
.map(voice => `<option value="${voice.name}">${voice.name} (${voice.lang})</option>`)
46+
.join('');
47+
voicesDropdown.innerHTML = voiceOptions;
48+
}
49+
function setVoice() {
50+
msg.voice = voices.find(voice => voice.name === this.value);
51+
toggle();
52+
}
53+
function toggle(startOver = true) {
54+
speechSynthesis.cancel();
55+
if (startOver) {
56+
speechSynthesis.speak(msg);
57+
}
58+
}
59+
function setOption(e) {
60+
console.log(this.name, this.value);
61+
msg[this.name] = this.value;
62+
toggle();
63+
}
64+
speechSynthesis.addEventListener('voiceschanged', populateVoices);
65+
voicesDropdown.addEventListener('change', setVoice);
66+
options.forEach(option => option.addEventListener('change', setOption));
67+
speakButton.addEventListener('click', toggle);
68+
stopButton.addEventListener('click', () => toggle(false));
3869
</script>
3970

4071
</body>

0 commit comments

Comments
 (0)