4

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 ファイルが返されます。

さまざまなユーザー エージェントを使用してみましたが、何も機能しないようです。

助けてください。

ありがとうございました

4

3 に答える 3

3

パスを文字列ではなく URI として使用してから、ASCII 文字列に変更します。

URI uri = new URI("http://translate.google.com/translate_tts?tl=zh-TW&q=你好");

URL u = new URL(uri.toASCIIString());
于 2012-04-26T14:23:19.167 に答える
1

私はあなたと同じ問題を抱えています。でも、昨日解決しました。APIに中国語を言って、mp3ファイルに保存したい。

現在の URL は次のとおりです: path ="http://translate.google.com/translate_tts?tl=zh-TW&q=你好" 次のようにします: path ="http://translate.google.com/translate_tts?ie =UTF-8&tl=zh-TW&q=".urlencode("你好");

パラメータ ie=utf-8 を追加し、中国語の単語をエンコードします。あなたが望むものを手に入れます。

于 2013-05-07T02:30:17.460 に答える
0

アプリがクラッシュした場合は、これを試してください

txtToTranslate = txtToTranslate.replace(" ", "%20");

単語間のスペースを置き換えます。

于 2013-09-27T00:19:22.667 に答える