Java:从Google Translate下载Text to Speech [英] Java: download Text to Speech from Google Translate
问题描述
我正在尝试使用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屋!