android – 适用于阿拉伯语,中文和希腊语的Google TTS API

android – 适用于阿拉伯语,中文和希腊语的Google TTS API,第1张

概述我想从谷歌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 target 我想从谷歌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,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文件.

我尝试过使用不同的用户代理,似乎没什么用.

请帮忙.

谢谢

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

URI uri = new URI("http://translate.Google.com/translate_tts?tl=zh-TW&q=你好");URL u = new URL(uri.toASCIIString());
总结

以上是内存溢出为你收集整理的android – 适用于阿拉伯语,中文和希腊语的Google TTS API全部内容,希望文章能够帮你解决android – 适用于阿拉伯语,中文和希腊语的Google TTS API所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1128583.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存