SpeechSynthesisUtterance: voice 属性
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 |
浏览器兼容性
加载中…