SpeechSynthesisUtterance: voice-Eigenschaft

Baseline Widely available

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

Die voice-Eigenschaft des SpeechSynthesisUtterance-Interfaces bekommt und setzt die Stimme, die verwendet wird, um den Text auszusprechen.

Diese sollte auf eines der SpeechSynthesisVoice-Objekte gesetzt werden, die von SpeechSynthesis.getVoices() zurückgegeben werden. Falls sie nicht gesetzt wird, bevor der Text gesprochen wird, wird die am besten geeignete Standardstimme für die lang-Einstellung des Textes verwendet.

Wert

Beispiele

js
const synth = window.speechSynthesis;

const inputForm = document.querySelector("form");
const inputTxt = document.querySelector("input");
const voiceSelect = document.querySelector("select");

const voices = synth.getVoices();

// …

inputForm.onsubmit = (event) => {
  event.preventDefault();

  const utterThis = new SpeechSynthesisUtterance(inputTxt.value);
  const selectedOption =
    voiceSelect.selectedOptions[0].getAttribute("data-name");
  for (const voice of voices) {
    if (voice.name === selectedOption) {
      utterThis.voice = voice;
    }
  }
  synth.speak(utterThis);
  inputTxt.blur();
};

Spezifikationen

Specification
Web Speech API
# dom-speechsynthesisutterance-voice

Browser-Kompatibilität

Siehe auch