{{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.
Properties
SpeechSynthesis
also inherits properties from its parent interface, {{domxref("EventTarget")}}.
- {{domxref("SpeechSynthesis.paused")}} {{readonlyinline}}
- A {{domxref("Boolean")}} that returns
true
if theSpeechSynthesis
object is in a paused state. - {{domxref("SpeechSynthesis.pending")}} {{readonlyinline}}
- A {{domxref("Boolean")}} that returns
true
if the utterance queue contains as-yet-unspoken utterances. - {{domxref("SpeechSynthesis.speaking")}} {{readonlyinline}}
- A {{domxref("Boolean")}} that returns
true
if an utterance is currently in the process of being spoken — even ifSpeechSynthesis
is in a paused state.
Event handlers
- {{domxref("SpeechSynthesis.onvoiceschanged")}}
- Fired when the list of {{domxref("SpeechSynthesisVoice")}} objects that would be returned by the {{domxref("SpeechSynthesis.getVoices()")}} method has changed.
Methods
SpeechSynthesis
also inherits methods from its parent interface, {{domxref("EventTarget")}}.
- {{domxref("SpeechSynthesis.cancel()")}}
- Removes all utterances from the utterance queue.
- {{domxref("SpeechSynthesis.getVoices()")}}
- Returns a list of {{domxref("SpeechSynthesisVoice")}} objects representing all the available voices on the current device.
- {{domxref("SpeechSynthesis.pause()")}}
- Puts the
SpeechSynthesis
object into a paused state. - {{domxref("SpeechSynthesis.resume()")}}
- Puts the
SpeechSynthesis
object into a non-paused state: resumes it if it was already paused. - {{domxref("SpeechSynthesis.speak()")}}
- Adds an {{domxref("SpeechSynthesisUtterance", "utterance")}} to the utterance queue; it will be spoken when any other utterances queued before it have been spoken.
Examples
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('input'); var voiceSelect = document.querySelector('select'); var voices = synth.getVoices(); for(i = 0; i < voices.length ; i++) { var option = document.createElement('option'); option.textContent = voices[i].name + ' (' + voices[i].lang + ')'; option.setAttribute('data-lang', voices[i].lang); option.setAttribute('data-name', voices[i].name); voiceSelect.appendChild(option); } 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]; } } synth.speak(utterThis); inputTxt.blur(); }
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('Web Speech API', '#tts-section', 'SpeechSynthesis')}} | {{Spec2('Web Speech API')}} |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | {{CompatChrome(33)}} | {{CompatGeckoDesktop(45)}} [1] | {{CompatNo}} | {{CompatUnknown}} | 7 |
Feature | Android | Chrome | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatNo}} | 2.0 | {{CompatNo}} | {{CompatNo}} | 7.1 |
- [1] Can be enabled via the
media.webspeech.synth.enabled
flag in about:config. Windows has been implemented on version 42, Mac and Linux have been implemented on version 44.