SpeechSynthesis.getVoices()
SpeechSynthesis
接口的 getVoices()
方法返回一个 SpeechSynthesisVoice
(en-US) 列表,用于表示当前设备上所有可用的语音。
语法
getVoices()
参数
无。
返回值
返回一个类型为 SpeechSynthesisVoice
(en-US) 的数组(array)列表(list)。
示例
JavaScript
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
<select id="voiceSelect"></select>
规范
Specification |
---|
Web Speech API # dom-speechsynthesis-getvoices |
浏览器兼容性
BCD tables only load in the browser