1

I am making an language app that needs audio as well as visual translations. I thought of using Google Translate API, but according to this SO post, it appears that "The explanation is that Google restricts the usage of this service."

But this SO post, Google Text-To-Speech API, says I can use tl and q params, like http://translate.google.com/translate_tts?tl=zh-CN&q=Hello to return audio. This gives me a weird transliteration in Chinese of "Hello", which says "Ha Low" instead of "Ni Hao".

The user also states that "it will automatically generate a wav file which you can easily get with an HTTP request". When I GET using jsonp (need to use this because $.get() or non-jsonp requests have origin blocked) I get the following unreadable error:

enter image description here

Code:

        var theApp = angular.module('myApp', []);
        theApp.controller('APICtrl', ['$scope', '$http',
            function($scope, $http) {
                var httpRequest = "http://translate.google.com/translate_tts?tl=" + "zh-CN" + "&q=" + "Hello";
                $http.jsonp(httpRequest) 
                    .success(function(data) {
                        console.log(data);
                    }
                );
            }
        ]);

So my question is... is there a standard endpoint for Google translate API to access audio files? If not, what is a good way to do this that works?

Community
  • 1
  • 1
user3871
  • 12,432
  • 33
  • 128
  • 268
  • 1
    jsonp only works on textual data, no binary support. do you need to save the audio bytes, or just play them? – dandavis Jun 02 '15 at 01:44
  • @dandavis What I need is... each time the user hovers over a foreign word, it will play the word audio. I could make a request each time to the API and play it on demand, but I was thinking that may be expensive... and was hoping to pre-specify commonly used words and store their audio files in an array. I suppose I could just cache words players commonly hover over? – user3871 Jun 02 '15 at 02:01
  • 1
    ive had luck using multiple audio tags and preload, but the API seems fast enough to not need to pre-load... – dandavis Jun 02 '15 at 04:48

1 Answers1

0

This is a late response...

I would suggest using Jquery (or vanilla JavaScript) to do this instead of putting it in your app's controller. The URL you provided is fine, but now you need to set the Referer and User-Agent headers before you make the request.

You can use Jquery.get() to download the mp3 from Google's TTS API (you have to set the headers!). Then, you can create an HTML5 Audio object out of the data returned by Jquery in the #success method, and play that.

The response size for one-word queries like Hello are around 2-3kb, plenty small enough. Google's API is also incredibly fast, so with those two factors combined I wouldn't worry about performance at all. Keep in mind that the browser will also cache the audio file, so any subsequent 'hovering over words' will use the cached version and not send another request.

Community
  • 1
  • 1
Chris Cirefice
  • 5,475
  • 7
  • 45
  • 75