Estou tentando reproduzir um texto em áudio!
Era para exibir uma lista com as vozes e línguas (Voz: Maria - Português "pt-BR", Voz: Anna - Inglês "en-US", ...) selecionar a desejada, escrever um texto e assim reproduzi-la em áudio. Porém estou tendo problemas e sem sucesso para resolver!
<body>
Selecionar Voz: <select id="voiceList"></select><br><br>
<input id="txtInput" /><br><br>
<button id="btnSpeak">Falar!</button>
<script>
// Selecionando as id do HTML
var txtInput = document.querySelector('#txtInput')
var voiceList = document.querySelector('#voiceList')
var btnSpeak = document.querySelector('#btnSpeak')
// sintetizador
var synth = window.SpeechSynthesis;
var voices = []
NewVoices()
if (speechSynthesis !== undefined) {
speechSynthesis.onvoiceschanged = NewVoices
}
btnSpeak.addEventListener('click', () => {
var toSpeak = new SpeechSynthesisUtterance(txtInput.value)
var selectedVoiceName = voiceList.selectedOptions[0].getnAttribute('data-name')
voices.forEach((voice) => {
if (voice.name === selectedVoiceName) {
toSpeak.voice = voice
}
})
synth.speak(toSpeak);
})
function NewVoices() {
voices = synth.getVoices()
var selectedIndex = voiceList.selectedIndex < 0 ? 0 : voiceList.selectedIndex
voiceList.inneHTML= ''
voices.forEach((voice) => {
var listItem = document.createElement('option')
listItem.textContent = voice.name
listItem.setAttribute('data-lang', voice.lang)
listItem.setAttribute('data-name', voice.name)
voiceList.appendChild(listItem)
})
voiceList.selectedIndex = selectedIndex
}
</script>
</body>
voiceList.selectedOptions[0].getnAttribute('data-name')
emgetnAttribute('data-name')
que está incorreta, seriagetAttribute('data-name')