SpeechSynthesis.getVoices()
>SpeechSynthesis 接口的 getVoices() 方法返回一个 SpeechSynthesisVoice 列表,用于表示当前设备上所有可用的语音。
语法
js
getVoices()
参数
无。
返回值
返回一个类型为 SpeechSynthesisVoice 的数组(array)列表(list)。
示例
>JavaScript
js
function populateVoiceList() {
if (typeof speechSynthesis === "undefined") {
return;
}
var voices = speechSynthesis.getVoices();
for (var i = 0; i < voices.length; i++) {
var option = document.createElement("option");
option.textContent = voices[i].name + " (" + voices[i].lang + ")";
if (voices[i].default) {
option.textContent += " -- DEFAULT";
}
option.setAttribute("data-lang", voices[i].lang);
option.setAttribute("data-name", voices[i].name);
document.getElementById("voiceSelect").appendChild(option);
}
}
populateVoiceList();
if (
typeof speechSynthesis !== "undefined" &&
speechSynthesis.onvoiceschanged !== undefined
) {
speechSynthesis.onvoiceschanged = populateVoiceList;
}
HTML
html
<select id="voiceSelect"></select>
规范
| 规范 |
|---|
| Web Speech API> # dom-speechsynthesis-getvoices> |