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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With