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

瀏覽器相容性

另見