How to use Google Translate TTS with the new V2 API?

I used Google Translate TTS to download the audio file using this URL: http://translate.google.com/translate_tts?tl=en&q=Hello+world !

However, Google has changed the way it works, and so I can no longer upload audio files. I signed up for a free trial of the Google Translate V2 API, but cannot find how to get the TTS audio files.

Any idea?

+12
source share
6 answers

Text-to-speech has always been an “unofficial” API, which is now password protected to prevent abuse. It has never been advertised as part of the translation API, and there is currently no TTS functionality in the V2 Translate API, paid or otherwise.

There are several more prerequisites for the following flow groups , which have been going on for some time.

+3
source

Updated from 03/03/2019 (dd / mm / yyyy) https://cloud.google.com/text-to-speech/ Official TTS from Google

0
source

Here is for those who are desperate to play Google TTS as audio in HTML: let me save you a couple of hours of time and tell you how to do it .

Let's say we have this link: https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=en&q=I+love+coffee

If you try to play this audio at the specified link and use <audio> , <iframe> using third-party libraries or play it using Javascript ...

var audio = new Audio('https://translate.google.com/translate_tts...'); audio.play();

... then you will soon find that none of the above methods work, as 404 error is thrown .

Decision

Apparently, the only possible way to play this universal TTS sound is to use a <embed> enclosed in a custom <iframe> and assign a unique version number to the link (this is important because browser caching does not allow the sound to play for some reason).

Here is the solution for our example: (if you have iframe # ttsiframe)

 function playTTS(lang,sentence) { //get the iframe var iFrame = document.getElementById('ttsiframe'); //remove its sandbox property iFrame.removeAttribute('sandbox'); //this is your reference variable for the iframe body and head tag var iFrameBody; //get the body if (iFrame.contentDocument) { // FF iFrameBody = iFrame.contentDocument.getElementsByTagName('body')[0]; iFrameHead = iFrame.contentDocument.getElementsByTagName('head')[0]; } else if (iFrame.contentWindow) { // IE iFrameBody = iFrame.contentWindow.document.getElementsByTagName('body')[0]; iFrameHead = iFrame.contentWindow.document.getElementsByTagName('head')[0]; } else { iFrameBody = iFrame.contentDocument.body; iFrameHead = iFrame.contentDocument.head; } //generate link to Google Translate TTS using arguments (pay attention to random version number at the end) var link = 'https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=' + lang + '&q=' + sentence.replace(/ /g,'+').replace(/[.]/g,'') + '&rd=' + getRandomInt(0,50000000); //add embed element with our link iFrameBody.innerHTML = '<embed src="' + link + '" id="TTS">'; //isolate iframe iFrame.setAttribute('sandbox',''); } 
0
source

You can simply use the link:

Text to speech

-1
source

Source: https://habr.com/ru/post/1241446/


All Articles