<div class="container">
<div class="form-group">
<label for="email">Select A Voice</label>
<select class="form-control"></select>
</div>
<div class="form-group">
<label for="email">Text To Read</label>
<textarea type="text" id="textToRead" class="form-control"></textarea>
</div>
<button class="btn btn-default" onclick="read()">Read</button>
</div>
body{
padding: 20px 0;
}
var utterance = new SpeechSynthesisUtterance();
utterance.pitch = 1.2; // 1 is default, range 0 to 2
utterance.rate = 1.4; //1 is default, range 0.1 to 10
utterance.volume = 1; //1 is default, range 0 to 1
utterance.lang = "en-US";
var voices = [];
window.speechSynthesis.onvoiceschanged = function() {
voices = window.speechSynthesis.getVoices();
for (i = 0; i < voices.length; i++) {
var option = document.createElement("option");
option.textContent = voices[i].name + " (" + voices[i].lang + ")";
if (voices[i].default) {
option.textContent += " -- DEFAULT";
}
option.setAttribute("data-lang", voices[i].lang);
option.setAttribute("data-name", voices[i].name);
document.querySelector("select").appendChild(option);
}
};
function read() {
var selectedVoice = document
.querySelector("select")
.selectedOptions[0].getAttribute("data-name");
for (i = 0; i < voices.length; i++) {
if (voices[i].name === selectedVoice) {
utterance.voice = voices[i];
}
}
utterance.text = document.getElementById("textToRead").value;
console.log(document.getElementById("textToRead").value)
window.speechSynthesis.speak(utterance);
}
This Pen doesn't use any external JavaScript resources.