Java:从Google Translate下载Text to Speech [英] Java: download Text to Speech from Google Translate

查看:126
本文介绍了Java:从Google Translate下载Text to Speech的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java从Google翻译下载文本到语音。它适用于英语,但使用日语则不成功。以下是我的代码:

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?

推荐答案

您似乎需要告诉Google搜索字词包含UTF-8编码字符。

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

将您的网址更改为 http://translate.google.com/translate_tts?ie=UTF-8&tl=ja&q= 为我解决了这个问题。与Google翻译网站的音频翻译相比,我获得了相同的 .mp3

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.

这篇关于Java:从Google Translate下载Text to Speech的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆