Search code examples
androidstringmedia-playergoogle-apiarabic

Using Arabic characters with google TTS API


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

Solution

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