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

238
Visualizações
How to play a base64 response from Google Text to Speech as an mp3 audio in browser using Javascript

I wrote this code to play an audio on a button click but I can't make it work. Google docs aren't helpful on handling the response. Can anyone please help?

async function playVoiceover() {
    const url ='https://texttospeech.googleapis.com/v1/text:synthesize?key=XXXXXXX'
    const rawResponse = await fetch(url, {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        'input':{
          'ssml':`<speak>The <say-as interpret-as=\"characters\">SSML</say-as>
               standard <break time=\"1s\"/>is defined by the
               <sub alias=\"World Wide Web Consortium\">W3C</sub>.</speak>`
         },
         'voice':{
           'languageCode':'en-us',
           'name':'en-US-Standard-B',
           'ssmlGender':'MALE'
         },
         'audioConfig':{
           'audioEncoding':'MP3'
         }
      })
    });
    const content = await rawResponse.json();
    const mp3 = 'data:audio/mp3;base64,'+window.atob(content.audioContent)
    const audio = new Audio(mp3);
    audio.play();
  }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I'm leaving this answer as information about this case for the community:

  • window.atob()

    The atob() method decodes a base-64 encoded string.

  • texttospeech.googleapis.com (response structure)

{ "audioContent": string } // A base64-encoded string.

Its not needed to decoded when you are formating the mp3 string with the codec structure. You can see a similar response here.

For additional details about the response from the texttospeech api:

  • text-to-speech -> synthesize -> response-body
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