Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
Javascript text-to-speech switch language problem

i have an issue changing dynamically the language of the text-to-speech, the first play is correct the second use the previous language. Here the code:

$('.audio-btn').click(function(){
            let m = $(this).data('content');
            let lang = $(this).data('lang');
            var msg = new SpeechSynthesisUtterance();
            var voices = window.speechSynthesis.getVoices();
            msg.voice = voices[1];
            msg.voiceURI = "native";
            msg.volume = 1;
            msg.text = m;
            msg.lang = lang;
            speechSynthesis.speak(msg);
          })

and here the button i use:

<button class="audio-btn" data-content="Hola como estas?" data-lang="es-ES"></button>
<button class="audio-btn" data-content="Salut, comment ca va?" data-lang="fr-FR"></button>

thanks

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I have rewritten your code a little. The problem with you was the binding. Try this:

Update

in the console log you will see the index of using pronunciation.

const btns = document.querySelectorAll('button')

btns.forEach((el) => {
el.addEventListener('click', e => {
const msg = new SpeechSynthesisUtterance();
const txt = e.target.getAttribute('data-content');
const lang = e.target.getAttribute('data-lang');     
const langCode = lang.split('-')[0]

let s = setSpeech();
s.then((voices) => {
  //const voices = window.speechSynthesis.getVoices();   
  console.log(voices.findIndex(a => a.lang == langCode));
  
  msg.voice = voices[voices.findIndex(a => a.lang == langCode)];
  msg.text = txt;
  msg.lang = lang;
  msg.volume = 1;
  msg.voiceURI = "native";
  window.speechSynthesis.speak(msg);            
}); 
  

  })
})



function setSpeech() {
  return new Promise(
function (resolve, reject) {
  let synth = window.speechSynthesis;
  let id;

  id = setInterval(() => {
    if (synth.getVoices().length !== 0) {
      resolve(synth.getVoices());
      clearInterval(id);
    }
  }, 10);
}
  )
}
<button class="audio-btn" data-content="Hola como estas?" data-lang="es-ES">es</button>
<button class="audio-btn" data-content="Salut, comment ca va?" data-lang="fr-FR">fr</button>
<button class="audio-btn" data-content="Hello World! How are you?" data-lang="en-EN">en</button>
<button class="audio-btn" data-content="Alles klar? Jawohl! " data-lang="de-DE">de</button>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda