SpeechSynthesisUtterance script with play, pause, stop buttons and selection of language and voice
SpeechSynthesisUtterance script with play, pause, stop buttons and selection of language and voice
HTML
<div id="SpeechSynthesis">Leggo il testo per te?
<div>
<button id="speechplay">Play</button>
<button id="speechpause">Pause</button>
<button id="speechstop">Stop</button>
</div>
<select id="voices">
</select>
<br><span id="speechwarning">(Sono sperimentale, abbi pazienza.)</span>
</div>
<p id="texttospeech">A hare was very popular with the other beasts who all claimed to be her friends. But one day she heard the hounds approaching and hoped to escape them by the aid of her many friends. So she went to the horse, and asked him to carry her away from the hounds on his back.
But he declined, stating that he had important work to do for his master. “He felt sure,” he said, “that all her other friends would come to her assistance.” She then applied to the bull, and hoped that he would repel the hounds with his horns. The bull replied: “I am very sorry, but I have an appointment with a lady; but I feel sure that our friend the goat will do what you want.”
The goat, however, feared that his back might do her some harm if he took her upon it. The ram, he felt sure, was the proper friend to apply to. So she went to the ram and told him the case.
The ram replied: “Another time, my dear friend. I do not like to interfere on the present occasion, as hounds have been known to eat sheep as well as hares.” The Hare then applied, as a last hope, to the calf, who regretted that he was unable to help her, as he did not like to take the responsibility upon himself, as so many older persons than himself had declined the task. By this time the hounds were quite near, and the Hare took to her heels and luckily escaped.
Moral of the story: He that has many friends, has no friends.</p>
CSS
#SpeechSynthesis{
border: 1px solid red;
border-radius: 5px;
width: auto;
float: right;
padding: 1%;
margin: 0px 0px 5px 5px;
text-align: right;
font-style: italic;
color: #999;
}
#speechplay{
cursor: pointer;
font-size: 1em;
border-radius: 5px;
color: blue;
text-decoration: none;
padding: 3px;
margin-top: 5px;
border: 1px solid blue;
}
#speechpause {
cursor: pointer;
font-size: 1em;
border-radius: 5px;
color: blue;
text-decoration: none;
padding: 3px;
margin-top: 5px;
border: 1px solid blue;
}
#speechstop{
cursor: pointer;
font-size: 1em;
border-radius: 5px;
color: red;
text-decoration: none;
padding: 3px;
margin-top: 5px;
border: 1px solid red;
}
#voices {
font-size: 1em;
border-radius: 5px;
color: blue;
text-decoration: none;
padding: 3px;
border: 1px solid blue;
margin-top: 5px;
}
#speechwarning{
font-size: 0.8em;
color: #777777;
font-style: italic;
}
JavaScript
//https://www.hongkiat.com/blog/text-to-speech/
onload = function() {
if ('speechSynthesis' in window){
var synth = speechSynthesis;
var flag = false;
//stop when change page ???(not sure)
if(synth.speaking){ /* stop narration */
/* for safari */
flag = false;
synth.cancel();
}
/* references to the buttons */
var playEle = document.querySelector('#speechplay');
var pauseEle = document.querySelector('#speechpause');
var stopEle = document.querySelector('#speechstop');
/* click event handlers for the buttons */
playEle.addEventListener('click', onClickPlay);
pauseEle.addEventListener('click', onClickPause);
stopEle.addEventListener('click', onClickStop);
// select voices////
//var synth = window.speechSynthesis;
var voiceSelect = document.querySelector('#voices');
var voices = [];
function populateVoiceList() {
voices = synth.getVoices().sort(function (a, b) {
const aname = a.name.toUpperCase(), bname = b.name.toUpperCase();
if ( aname < bname ) return -1;
else if ( aname == bname ) return 0;
else return +1;
});
var selectedIndex = voiceSelect.selectedIndex < 0 ? 0 : voiceSelect.selectedIndex;
voiceSelect.innerHTML = '';
//get cookie voice
//var cookievoice = getCookie('SpeechSynthesisVoice');
var cookievoice = sessionStorage.getItem("SpeechSynthesisVoice");
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);
//add selcted to option if if cookievoice
if (cookievoice === voices[i].name) {
selectedIndex = i;
...