Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!