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

浏览器兼容性

另见