SpeechSynthesis: getVoices()-Methode
Baseline
Weitgehend verfügbar
Diese Funktion ist gut etabliert und funktioniert auf vielen Geräten und in vielen Browserversionen. Sie ist seit September 2018 browserübergreifend verfügbar.
Die Methode getVoices() der Schnittstelle SpeechSynthesis gibt eine Liste von SpeechSynthesisVoice-Objekten zurück, die alle auf dem aktuellen Gerät verfügbaren Stimmen repräsentieren.
Syntax
js
getVoices()
Parameter
Keine.
Rückgabewert
Eine Liste (ein Array) von SpeechSynthesisVoice-Objekten.
Beispiele
>HTML
html
<select id="voiceSelect"></select>
JavaScript
js
function populateVoiceList() {
if (typeof speechSynthesis === "undefined") {
return;
}
const voices = speechSynthesis.getVoices();
for (const voice of voices) {
const option = document.createElement("option");
option.textContent = `${voice.name} (${voice.lang})`;
if (voice.default) {
option.textContent += " — DEFAULT";
}
option.setAttribute("data-lang", voice.lang);
option.setAttribute("data-name", voice.name);
document.getElementById("voiceSelect").appendChild(option);
}
}
populateVoiceList();
if (
typeof speechSynthesis !== "undefined" &&
speechSynthesis.onvoiceschanged !== undefined
) {
speechSynthesis.onvoiceschanged = populateVoiceList;
}
Spezifikationen
| Spezifikation |
|---|
| Web Speech API> # dom-speechsynthesis-getvoices> |