Using Arabic characters with google TTS API

﹥>﹥吖頭↗ 提交于 2019-12-25 08:55:12

问题


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

回答1:


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



回答2:


The reason about encoding url, try this:

URI uri = new URI(""http://translate.google.com/translate_tts?tl=" + oLanguage + "&q=" + mainText");

player.setDataSource(uri.toASCIIString());



来源:https://stackoverflow.com/questions/10220324/using-arabic-characters-with-google-tts-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!