1

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();
  }
  • I think this question is a duplicate of this: https://stackoverflow.com/questions/17762763/play-wav-sound-file-encoded-in-base64-with-javascript Probably removing window.atob() will fix it. Same for this: https://stackoverflow.com/questions/50312008/play-audio-file-from-string-in-javascript – Allart Nov 16 '21 at 10:10
  • You can also take a look at this: https://stackoverflow.com/a/22234035/11592836 – Allart Nov 16 '21 at 10:16
  • Tested on my side. Allart is right, just remove window.atob() and issue is solve! If you answer it @allart I will thumb up. – Betjens Nov 16 '21 at 20:12

1 Answers1

2

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:

Betjens
  • 1,353
  • 2
  • 4
  • 13