Google TTS API から mp3 ファイルをダウンロードしようとしています。コードは次のとおりです。
try {
String path ="http://translate.google.com/translate_tts?tl=en&q=hello";
//this is the name of the local file you will create
String targetFileName = "test.mp3";
boolean eof = false;
URL u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.addRequestProperty("User-Agent", "Mozilla/5.0");
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream f = new FileOutputStream(new File(Environment.getExternalStorageDirectory()
+ "/download/"+targetFileName));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = in.read(buffer)) > 0 ) {
f.write(buffer,0, len1);
}
f.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
これは問題なく動作しますが、特殊文字を使用する中国語やギリシャ語などの言語をリクエストしようとすると
String path ="http://translate.google.com/translate_tts?tl=zh-TW&q=你好";
返された mp3 ファイルには音声がありませんが、ファイルのサイズからデータが含まれていることがわかります。アラビア語で同じことをしようとすると
String path ="http://translate.google.com/translate_tts?tl=ar&q=%D8%A7%D9%84%D9%84%D9%87";
0 バイトの空の mp3 ファイルが返されます。
さまざまなユーザー エージェントを使用してみましたが、何も機能しないようです。
助けてください。
ありがとうございました