Home > Enterprise >  How to get an audio response from an API call within google app to play in speakers
How to get an audio response from an API call within google app to play in speakers

Time:04-18

In my googlesheets app script (I am new to google sheets, but so glad they built off of javascript!), I wish reuse one of my javascript functions that works fine in my browser:

function playSoundGoogleTTS(greekText) {
   const greekTtsUrl= `https://translate.google.com/translate_tts?ie=UTF-8&tl=el&client=tw-ob&q=${greekText}`;
   // use the browser's audio 
   let audioPlayerElem = document.getElementById("audioPlayer");
   audioPlayerElem.src = ttsUrl;
   audioPlayerElem.play();
}

I have rewritten the above function, since this is not running in a browser to use the recommended UrlFetchApp:

function playSoundGoogleTTS(greekText, langPronValue) {
   const greekTtsUrl= `https://translate.google.com/translate_tts?ie=UTF-8&tl=el&client=tw-ob&q=${greekText}`;
   // NOT using browser to play audio
   var googleResp = UrlFetchApp.fetch(ttsUrl);
   console.log(googleResp);
}

but although this returns successfully, there is no sound redirected to my speakers. I have no way to debug this either that I know of, the way I could with chrome dev tools.

I have searched the docs for UrlFetchApp and find nothing to help me here. I have googled around retrieving the blob response from UrlFetchApp and then trying to play this explicitly, but find no pointers. Any ideas?

CodePudding user response:

You will have to create a dialog, sidebar or web app using the HTML Service in order to be able to access to the audio player devices connected to your device.

The above because Google Apps Script executes code on the server side and it hasn't access to Web APIs including the one required to play audio files.

Resources

Related

  • Related