SpeechSynthesisUtterance: voice 属性

Baseline 已广泛支持

此功能已成熟,并可在多种设备和浏览器版本上使用。自 2018 年 9 月以来,它已在各种浏览器中推出。

voice 属性是 SpeechSynthesisUtterance 接口的一部分,用于获取和设置将用于朗读发音的语音。

在发音朗读之前,应将其设置为由 SpeechSynthesis.getVoices() 返回的 SpeechSynthesisVoice 对象之一。如果发音朗读时未设置此属性,则将使用适合发音的 lang 设置的默认语音。

一个 SpeechSynthesisVoice 对象。

示例

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

规范

规范
Web Speech API
# dom-speechsynthesisutterance-voice

浏览器兼容性

另见