用于阿拉伯语、中文和希腊语的 Google TTS API [英] Google TTS API for Arabic, Chinese and Greek

查看:39
本文介绍了用于阿拉伯语、中文和希腊语的 Google TTS API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从谷歌 TTS API 下载 mp3 文件,这是代码

I am trying to download an mp3 file from google TTS API, here is the code

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();


    }

这很好用,但是当我尝试对使用特殊字符的中文或希腊语等语言提出请求时

This works fine, but when I try to make the request for languages like chinese or greek which use special characters

String path ="http://translate.google.com/translate_tts?tl=zh-TW&q=你好";

我取回的 mp3 文件没有声音,但从文件的大小我可以看出里面有数据.当我用阿拉伯语尝试相同时

The mp3 file I get back has no sound but from the size of the file I can tell it has data in it. When I try the same with Arabic

String path ="http://translate.google.com/translate_tts?tl=ar&q=%D8%A7%D9%84%D9%84%D9%87";

我得到一个 0 字节的空 mp3 文件.

I get back an empty mp3 file with 0 bytes.

我尝试使用不同的用户代理,但似乎没有任何效果.

I have tried using different user agents and nothing seems to work.

请帮忙.

谢谢

推荐答案

使用路径作为 URI 而不是字符串,然后将其更改为 ascii 字符串.

Use the path as a URI rather than a string then change it to an ascii string.

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

URL u = new URL(uri.toASCIIString());

这篇关于用于阿拉伯语、中文和希腊语的 Google TTS API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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