I have not being able to use the getvoices()
function from the speechsynthesis
framework.
My basic idea is to be able to click on the document and be able to read the text of the element in Spanish. Therefore, I am trying to use the promise and await paradigm to set the voice variable upon loading the document based on the list of available voices.
What am I missing? thanks!
var synth = window.speechSynthesis;
function getVoices() {
return new Promise(resolve => {
let voices = speechSynthesis.getVoices()
resolve(voices)
})
}
async function getVoz() {
const Voces = await getVoices();
for(voz of Voces){
if (voz.lang=="es-ES"){
return voz
}
}
}
var voice = getVoz()
function sayit(){
var div = document.getElementById('main')
var what=div.innerHTML
var hi = new SpeechSynthesisUtterance(what);
hi.pitch = 1;
hi.rate = 1;
hi = voice;
synth.speak(hi);
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="script.js"></script>
</head>
<body>
<div id="main" onclick="sayit()"> Hola Mundo </div>
</body>
</html>