Speech API

by Ben Gillbanks

HTML

<button id="say-hello">Hello</button>

JavaScript

const synth = window.speechSynthesis;
document.getElementById('say-hello').addEventListener(
	'click', 
    function(e) {
	    console.log(synth.getVoices());

    	const utterThis = new SpeechSynthesisUtterance(`Hello. I love sausage.`);
        utterThis.voice=getVoice('Tingting');
		synth.speak(utterThis);
        
	}
);

function getVoice(name) {
	const voices = synth.getVoices();
    for (let i = 0; i < voices.length ; i++) {
    	if (voices[i].name === name) {
        	return voices[i];
		}
    }
}