This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
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, EventTarget
.
SpeechSynthesis.paused
Read only- A
Boolean
that returnstrue
if theSpeechSynthesis
object is in a paused state. SpeechSynthesis.pending
Read only- A
Boolean
that returnstrue
if the utterance queue contains as-yet-unspoken utterances. SpeechSynthesis.speaking
Read only- A
Boolean
that returnstrue
if an utterance is currently in the process of being spoken — even ifSpeechSynthesis
is in a paused state.
Methods
SpeechSynthesis
also inherits methods from its parent interface, EventTarget
.
SpeechSynthesis.cancel()
- Removes all utterances from the utterance queue.
SpeechSynthesis.getVoices()
- Returns a list of
SpeechSynthesisVoice
objects representing all the available voices on the current device. SpeechSynthesis.pause()
- Puts the
SpeechSynthesis
object into a paused state. SpeechSynthesis.resume()
- Puts the
SpeechSynthesis
object into a non-paused state: resumes it if it was already paused. SpeechSynthesis.speak()
- Adds an
utterance
to the utterance queue; it will be spoken when any other utterances queued before it have been spoken.
Events
Listen to this event using addEventListener()
or by assigning an event listener to the oneventname
property of this interface.
voiceschanged
- Fired when the list of
SpeechSynthesisVoice
objects that would be returned by theSpeechSynthesis.getVoices()
method has changed.
Also available via theonvoiceschanged
property.
Examples
First, a simple example:
let utterance = new SpeechSynthesisUtterance("Hello world!"); speechSynthesis.speak(utterance);
Now we'll look at a more fully-fledged example. In our 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 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 SpeechSynthesisUtterance
instance containing the text from the text <input>
, set the utterance's voice to the voice selected in the <select>
element, and start the utterance speaking via the 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(); }
Specifications
Specification | Status | Comment |
---|---|---|
Web Speech API The definition of 'SpeechSynthesis' in that specification. |
Draft |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
SpeechSynthesis | Chrome Full support 33 | Edge Full support ≤18 | Firefox Full support 49 | IE No support No | Opera Full support 21 | Safari Full support 7 | WebView Android Full support 4.4.3 | Chrome Android Full support 33 | Firefox Android
Full support
62
| Opera Android No support No | Safari iOS Full support 7 | Samsung Internet Android Full support 3.0 |
cancel | Chrome Full support 33 | Edge Full support 14 | Firefox Full support 49 | IE No support No | Opera Full support 21 | Safari Full support 7 | WebView Android Full support 4.4.3 | Chrome Android Full support 33 | Firefox Android
Full support
62
| Opera Android No support No | Safari iOS Full support 7 | Samsung Internet Android Full support 3.0 |
getVoices | Chrome Full support 33 | Edge Full support 14 | Firefox Full support 49 | IE No support No | Opera Full support 21 | Safari Full support 7 | WebView Android Full support 4.4.3 | Chrome Android Full support 33 | Firefox Android
Full support
62
| Opera Android No support No | Safari iOS Full support 7 | Samsung Internet Android Full support 3.0 |
onvoiceschanged | Chrome Full support 33 | Edge Full support 14 | Firefox Full support 49 | IE No support No | Opera No support No | Safari No support No | WebView Android Full support 4.4.3 | Chrome Android Full support 33 | Firefox Android
Full support
62
| Opera Android No support No | Safari iOS No support No | Samsung Internet Android Full support 3.0 |
pause | Chrome Full support 33 | Edge Full support 14 | Firefox Full support 49 | IE No support No | Opera Full support 21 | Safari Full support 7 | WebView Android
Full support
4.4.3
| Chrome Android
Full support
33
| Firefox Android
Full support
62
| Opera Android No support No | Safari iOS Full support 7 | Samsung Internet Android
Full support
3.0
|
paused | Chrome Full support 33 | Edge Full support 14 | Firefox Full support 49 | IE No support No | Opera Full support 21 | Safari Full support 7 | WebView Android Full support 4.4.3 | Chrome Android Full support 33 | Firefox Android
Full support
62
| Opera Android No support No | Safari iOS Full support 7 | Samsung Internet Android Full support 3.0 |
pending | Chrome Full support 33 | Edge Full support 14 | Firefox Full support 49 | IE No support No | Opera Full support 21 | Safari Full support 7 | WebView Android Full support 4.4.3 | Chrome Android Full support 33 | Firefox Android
Full support
62
| Opera Android No support No | Safari iOS Full support 7 | Samsung Internet Android Full support 3.0 |
resume | Chrome Full support 33 | Edge Full support 14 | Firefox Full support 49 | IE No support No | Opera Full support 21 | Safari Full support 7 | WebView Android Full support 4.4.3 | Chrome Android Full support 33 | Firefox Android
Full support
62
| Opera Android No support No | Safari iOS Full support 7 | Samsung Internet Android Full support 3.0 |
speak | Chrome Full support 33 | Edge Full support 14 | Firefox Full support 49 | IE No support No | Opera Full support 21 | Safari Full support 7 | WebView Android Full support 4.4.3 | Chrome Android Full support 33 | Firefox Android
Full support
62
| Opera Android No support No | Safari iOS Full support 7 | Samsung Internet Android Full support 3.0 |
speaking | Chrome Full support 33 | Edge Full support 14 | Firefox Full support 49 | IE No support No | Opera Full support 21 | Safari Full support 7 | WebView Android Full support 4.4.3 | Chrome Android Full support 33 | Firefox Android
Full support
62
| Opera Android No support No | Safari iOS Full support 7 | Samsung Internet Android Full support 3.0 |
voiceschanged event | Chrome Full support 33 | Edge Full support 14 | Firefox Full support 49 | IE No support No | Opera Full support 21 | Safari Full support 7 | WebView Android Full support 4.4.3 | Chrome Android Full support 33 | Firefox Android
Full support
62
| Opera Android No support No | Safari iOS Full support 7 | Samsung Internet Android Full support 3.0 |
Legend
- Full support
- Full support
- No support
- No support
- Experimental. Expect behavior to change in the future.
- Experimental. Expect behavior to change in the future.
- See implementation notes.
- See implementation notes.
- User must explicitly enable this feature.
- User must explicitly enable this feature.