SpeechSynthesisUtterance:volume 屬性

Baseline 已廣泛支援

此功能已成熟,並可在多種裝置和瀏覽器版本上使用。自 2018 年 9 月以來,它已在各種瀏覽器中推出。

volume 屬性是 SpeechSynthesisUtterance 介面的一個屬性,用於獲取和設定語音合成發音時的音量。

如果未設定,則使用預設值 1。

一個浮點數,表示音量值,範圍在 0(最低)到 1(最高)之間。

如果使用了 SSML,此值將被標記中的 prosody 標籤覆蓋。

示例

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;
    }
  }
  utterThis.volume = 0.5;
  synth.speak(utterThis);
  inputTxt.blur();
};

規範

規範
Web Speech API
# dom-speechsynthesisutterance-volume

瀏覽器相容性

另見