1
I’m trying to play an audio text!
It was to display a list of voices and languages (Voice: Maria - Portuguese "en-BR", Voice: Anna - English "en-US", ...) select the desired one, write a text and play it in audio. However I am having problems and unsuccessfully to solve!
<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>
I already took a look at this example: https://github.com/rbmelolima/SpeechSynthesis ?
– Leonardo Negrão
what problems are you having? submit a [mcve]
– Leandro Angelo
Most likely on this line:
voiceList.selectedOptions[0].getnAttribute('data-name')
ingetnAttribute('data-name')
which is incorrect, would begetAttribute('data-name')
– gleisin-dev