Esta página foi traduzida do inglês pela comunidade. Saiba mais e junte-se à comunidade MDN Web Docs.

View in English Always switch to English

SpeechSynthesisUtterance.voice

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨setembro de 2018⁩.

Experimental: Esta é uma tecnologia experimental
Verifique a tabela de compatibilidade entre Navegadores cuidadosamente antes de usar essa funcionalidade em produção.

A propriedade voice da interface SpeechSynthesisUtterance retorna e configura a voz que será usada para a fala.

Essa propriedade deve ser configurada para um dos objetos SpeechSynthesisVoice retornado por SpeechSynthesis.getVoices(). Se não for configurada no momento da fala, a voz usada será a determinada como default na propriedade lang.

Sintaxe

var myVoice = speechSynthesisUtteranceInstance.voice;
speechSynthesisUtteranceInstance.voice = speechSynthesisVoiceInstance;

Valor

Um objeto SpeechSynthesisVoice.

Exemplo

js
var synth = window.speechSynthesis;

var inputForm = document.querySelector('form');
var inputTxt = document.querySelector('input');
var voiceSelect = document.querySelector('select');

var voices = synth.getVoices();

  ...

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();
}

Especificações

Specification
Web Speech API
# dom-speechsynthesisutterance-voice

Compatibilidade com navegadores

Veja também