SpeechSynthesis

Cette traduction est incomplète. Aidez-nous à traduire cet article depuis l’anglais

Cette fonction est expérimentale
Puisque cette fonction est toujours en développement dans certains navigateurs, veuillez consulter le tableau de compatibilité pour les préfixes à utiliser selon les navigateurs.
Il convient de noter qu'une fonctionnalité expérimentale peut voir sa syntaxe ou son comportement modifié dans le futur en fonction des évolutions de la spécification.

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 Lecture seule
A Boolean that returns true if the SpeechSynthesis object is in a paused state.
SpeechSynthesis.pending Lecture seule
A Boolean that returns true if the utterance queue contains as-yet-unspoken utterances.
SpeechSynthesis.speaking Lecture seule
A Boolean that returns true if an utterance is currently in the process of being spoken — even if SpeechSynthesis 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 the SpeechSynthesis.getVoices() method has changed.
Also available via the onvoiceschanged property.

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 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 rate = document.querySelector('#rate');

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-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
La définition de 'SpeechSynthesis' dans cette spécification.
Projet

Browser compatibility

Update compatibility data on GitHub
OrdinateurMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariWebview AndroidChrome pour AndroidFirefox pour AndroidOpera pour AndroidSafari sur iOSSamsung Internet
SpeechSynthesis
Expérimentale
Chrome Support complet 33Edge Support complet OuiFirefox Support complet 49IE Aucun support NonOpera Support complet 21Safari Support complet 7WebView Android Support complet 4.4.3Chrome Android Support complet 33Firefox Android Support complet 62
Support complet 62
Aucun support 61 — 62
Désactivée
Désactivée From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Aucun support NonSafari iOS Support complet 7Samsung Internet Android Support complet 3.0
cancel
Expérimentale
Chrome Support complet 33Edge Support complet 14Firefox Support complet 49IE Aucun support NonOpera Support complet 21Safari Support complet 7WebView Android Support complet 4.4.3Chrome Android Support complet 33Firefox Android Support complet 62
Support complet 62
Aucun support 61 — 62
Désactivée
Désactivée From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Aucun support NonSafari iOS Support complet 7Samsung Internet Android Support complet 3.0
getVoices
Expérimentale
Chrome Support complet 33Edge Support complet 14Firefox Support complet 49IE Aucun support NonOpera Support complet 21Safari Support complet 7WebView Android Support complet 4.4.3Chrome Android Support complet 33Firefox Android Support complet 62
Support complet 62
Aucun support 61 — 62
Désactivée
Désactivée From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Aucun support NonSafari iOS Support complet 7Samsung Internet Android Support complet 3.0
onvoiceschanged
Expérimentale
Chrome Support complet 33Edge Support complet 14Firefox Support complet 49IE Aucun support NonOpera Aucun support NonSafari Aucun support NonWebView Android Support complet 4.4.3Chrome Android Support complet 33Firefox Android Support complet 62
Support complet 62
Aucun support 61 — 62
Désactivée
Désactivée From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Aucun support NonSafari iOS Aucun support NonSamsung Internet Android Support complet 3.0
pause
Expérimentale
Chrome Support complet 33Edge Support complet 14Firefox Support complet 49IE Aucun support NonOpera Support complet 21Safari Support complet 7WebView Android Support complet 4.4.3
Notes
Support complet 4.4.3
Notes
Notes In Android, pause() ends the current utterance. pause() behaves the same as cancel().
Chrome Android Support complet 33
Notes
Support complet 33
Notes
Notes In Android, pause() ends the current utterance. pause() behaves the same as cancel().
Firefox Android Support complet 62
Notes
Support complet 62
Notes
Notes In Android, pause() ends the current utterance. pause() behaves the same as cancel().
Aucun support 61 — 62
Notes Désactivée
Notes In Android, pause() ends the current utterance. pause() behaves the same as cancel().
Désactivée From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Aucun support NonSafari iOS Support complet 7Samsung Internet Android Support complet 3.0
Notes
Support complet 3.0
Notes
Notes In Android, pause() ends the current utterance. pause() behaves the same as cancel().
paused
Expérimentale
Chrome Support complet 33Edge Support complet 14Firefox Support complet 49IE Aucun support NonOpera Support complet 21Safari Support complet 7WebView Android Support complet 4.4.3Chrome Android Support complet 33Firefox Android Support complet 62
Support complet 62
Aucun support 61 — 62
Désactivée
Désactivée From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Aucun support NonSafari iOS Support complet 7Samsung Internet Android Support complet 3.0
pending
Expérimentale
Chrome Support complet 33Edge Support complet 14Firefox Support complet 49IE Aucun support NonOpera Support complet 21Safari Support complet 7WebView Android Support complet 4.4.3Chrome Android Support complet 33Firefox Android Support complet 62
Support complet 62
Aucun support 61 — 62
Désactivée
Désactivée From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Aucun support NonSafari iOS Support complet 7Samsung Internet Android Support complet 3.0
resume
Expérimentale
Chrome Support complet 33Edge Support complet 14Firefox Support complet 49IE Aucun support NonOpera Support complet 21Safari Support complet 7WebView Android Support complet 4.4.3Chrome Android Support complet 33Firefox Android Support complet 62
Support complet 62
Aucun support 61 — 62
Désactivée
Désactivée From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Aucun support NonSafari iOS Support complet 7Samsung Internet Android Support complet 3.0
speak
Expérimentale
Chrome Support complet 33Edge Support complet 14Firefox Support complet 49IE Aucun support NonOpera Support complet 21Safari Support complet 7WebView Android Support complet 4.4.3Chrome Android Support complet 33Firefox Android Support complet 62
Support complet 62
Aucun support 61 — 62
Désactivée
Désactivée From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Aucun support NonSafari iOS Support complet 7Samsung Internet Android Support complet 3.0
speaking
Expérimentale
Chrome Support complet 33Edge Support complet 14Firefox Support complet 49IE Aucun support NonOpera Support complet 21Safari Support complet 7WebView Android Support complet 4.4.3Chrome Android Support complet 33Firefox Android Support complet 62
Support complet 62
Aucun support 61 — 62
Désactivée
Désactivée From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Aucun support NonSafari iOS Support complet 7Samsung Internet Android Support complet 3.0
voiceschanged event
Expérimentale
Chrome Support complet 33Edge Support complet 14Firefox Support complet 49IE Aucun support NonOpera Support complet 21Safari Support complet 7WebView Android Support complet 4.4.3Chrome Android Support complet 33Firefox Android Support complet 62
Support complet 62
Aucun support 61 — 62
Désactivée
Désactivée From version 61 until version 62 (exclusive): this feature is behind the media.webspeech.synth.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Aucun support NonSafari iOS Support complet 7Samsung Internet Android Support complet 3.0

Légende

Support complet  
Support complet
Aucun support  
Aucun support
Fonctionnalité expérimentale. Celle-ci peut être amenée à changer par la suite.
Fonctionnalité expérimentale. Celle-ci peut être amenée à changer par la suite.
Voir les notes d'implémentation.
Voir les notes d'implémentation.
Une action explicite de l'utilisateur est nécessaire pour activer cette fonctionnalité.
Une action explicite de l'utilisateur est nécessaire pour activer cette fonctionnalité.

See also