--- title: SpeechSynthesis slug: Web/API/SpeechSynthesis tags: - API - Experimental - Interface - NeedsTranslation - Reference - SpeechSynthesis - TopicStub - Web Speech API - speech - synthesis translation_of: Web/API/SpeechSynthesis ---
{{APIRef("Web Speech API")}}{{SeeCompatTable}}
The SpeechSynthesis
interface of the Web Speech API is the controller interface for the speech service; this can be used to retrieve information about the synthesis voices available on the device, start and pause speech, and other commands besides.
SpeechSynthesis
also inherits properties from its parent interface, {{domxref("EventTarget")}}.
true
if the SpeechSynthesis
object is in a paused state.true
if the utterance queue contains as-yet-unspoken utterances.true
if an utterance is currently in the process of being spoken — even if SpeechSynthesis
is in a paused state.SpeechSynthesis
also inherits methods from its parent interface, {{domxref("EventTarget")}}.
SpeechSynthesis
object into a paused state.SpeechSynthesis
object into a non-paused state: resumes it if it was already paused.Listen to this event using addEventListener()
or by assigning an event listener to the oneventname
property of this interface.
voiceschanged
onvoiceschanged
property.In our basic Speech synthesiser demo, we first grab a reference to the SpeechSynthesis controller using window.speechSynthesis
. After defining some necessary variables, we retrieve a list of the voices available using {{domxref("SpeechSynthesis.getVoices()")}} and populate a select menu with them so the user can choose what voice they want.
Inside the inputForm.onsubmit
handler, we stop the form submitting with preventDefault(), create a new {{domxref("SpeechSynthesisUtterance")}} instance containing the text from the text {{htmlelement("input")}}, set the utterance's voice to the voice selected in the {{htmlelement("select")}} element, and start the utterance speaking via the {{domxref("SpeechSynthesis.speak()")}} method.
var synth = window.speechSynthesis; var inputForm = document.querySelector('form'); var inputTxt = document.querySelector('.txt'); var voiceSelect = document.querySelector('select'); var pitch = document.querySelector('#pitch'); var pitchValue = document.querySelector('.pitch-value'); var rate = document.querySelector('#rate'); var rateValue = document.querySelector('.rate-value'); var voices = []; function populateVoiceList() { voices = synth.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); voiceSelect.appendChild(option); } } populateVoiceList(); if (speechSynthesis.onvoiceschanged !== undefined) { speechSynthesis.onvoiceschanged = populateVoiceList; } inputForm.onsubmit = function(event) { event.preventDefault(); var utterThis = new SpeechSynthesisUtterance(inputTxt.value); var selectedOption = voiceSelect.selectedOptions[0].getAttribute('data-name'); for(i = 0; i < voices.length ; i++) { if(voices[i].name === selectedOption) { utterThis.voice = voices[i]; } } utterThis.pitch = pitch.value; utterThis.rate = rate.value; synth.speak(utterThis); inputTxt.blur(); }
Specification | Status | Comment |
---|---|---|
{{SpecName('Web Speech API', '#tts-section', 'SpeechSynthesis')}} | {{Spec2('Web Speech API')}} |
{{Compat("api.SpeechSynthesis")}}