Java: download Text to Speech from Google Translat

2019-04-09 07:51发布

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?

1条回答
forever°为你锁心
2楼-- · 2019-04-09 08:23

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.

查看更多
登录 后发表回答