在 google TTS API 中使用阿拉伯字符 [英] Using Arabic characters with google TTS API

查看:32
本文介绍了在 google TTS API 中使用阿拉伯字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用谷歌 TTS API,我想用阿拉伯语进行查询,但不断收到错误,我已将文本文件编码设置为 UTF-8,这是我的代码

I'm using the google TTS API and I want to make a query in Arabic but keep getting errors and I have set the text file encoding set to UTF-8, here is my code

    String oLanguage = "ar";
    MediaPlayer player = new MediaPlayer();
    String mainText = "الله";
    try {           
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.setDataSource("http://translate.google.com/translate_tts?tl=" + oLanguage + "&q=" + mainText);
        player.prepare();
        player.start();

    } catch (Exception e) {
        // TODO: handle exception
    }

当我使用其他语言时代码工作正常,但当我使用阿拉伯语时出现此错误

The code works fine when I use other languages but when I use Arabic I get this error

04-19 01:04:10.221: E/MediaPlayer(665): error (1, -2147483648)

另一件事是,当我尝试将阿拉伯语代码直接用于链接时,它会像这样显示

Another thing is when I try using the arabic code directly into the link it shows up like this

player.setDataSource("http://translate.google.com/translate_tts?tl=ar&q=%D8%A7%D9%84%D9%84%D9%87");

推荐答案

经过长时间的尝试解决此问题后,我找到的唯一解决方案是在发出类似于浏览器的请求之前更改用户代理.

Well after long hours of trying to fix this the only solution I found was to change the user agent before making the request to resemble that of a browser.

然而,根据我的研究,非常复杂地改变了媒体播放器的用户代理,所以我决定先下载文件,然后再播放.在每种情况下,用户代理仍然需要使用下面的行进行更改

However from my research changing the user-agent of the media player with very complicated so I decided to download the file first and then play it. In each case the User Agent still needs to be changed with the line below

(HttpURLConnection Object).addRequestProperty("User-Agent", "Mozilla/5.0");

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

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