Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: download Text to Speech from Google Translate

I am trying to download text to speech from Google Translate using Java. It works fine with English language, but with Japanese it is not successful. Following is my code:

try{
            String word="〜のそばに";
            word=java.net.URLEncoder.encode(word, "UTF-8");
            URL url = new URL("http://translate.google.com/translate_tts?tl=ja&q="+word);
            HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
            urlConn.addRequestProperty("User-Agent", "Mozilla/4.76");
            InputStream audioSrc = urlConn.getInputStream();
            DataInputStream read = new DataInputStream(audioSrc);
            OutputStream outstream = new FileOutputStream(new File("mysound.mp3"));
            byte[] buffer = new byte[1024];
            int len;
            while ((len = read.read(buffer)) > 0) {
                    outstream.write(buffer, 0, len);                    
            }
            outstream.close();              
}catch(IOException e){
           System.out.println(e.getMessage());
}

Do you have any idea or suggestion?

like image 288
DavidNg Avatar asked Nov 12 '12 06:11

DavidNg


People also ask

Can Google Translate audio files to text?

One of the features of Google Translate is that it allows you to transcribe voice to text. This comes in handy as it saves you the trouble of typing out the text. You can simply record the audio you wish to transcribe, and the app will convert it to text in your preferred language.


1 Answers

It seems that you need to tell Google that the search term contains UTF-8 encoded characters.

Changing your URL to http://translate.google.com/translate_tts?ie=UTF-8&tl=ja&q= fixes the problem for me. I get the same .mp3 downloaded as compared to the audio translation from the Google Translate site.

like image 149
andyb Avatar answered Nov 06 '22 19:11

andyb