1

An exception is thrown when I run this code. If you replace the Hindi characters in the URL with "Hello" it plays the file just fine.

When I load this URL (with the Hindi characters) in a browser it plays just fine.

What's going on?

Here's my code:

MediaPlayer mediaPlayer = new MediaPlayer();

mediaPlayer.setDataSource(getResources().getString(R.string.test)); 

mediaPlayer.prepare(); 

Here's the string resource def:

<string name="test">http://translate.google.com/translate_tts?q=आलू</string>
mellowg
  • 1,786
  • 3
  • 15
  • 19
  • hi mellowg, do you have found the solution to play the sound with that URL in mediaplayer? I am also facing same problem. – gypsicoder Aug 11 '11 at 16:33

3 Answers3

2

I don't think unicode characters are legal in URLs, unless you encode them. Here's the spec: https://www.rfc-editor.org/rfc/rfc1738

Community
  • 1
  • 1
tdammers
  • 20,353
  • 1
  • 39
  • 56
1

+1 tdammers is right, you can't have non-ASCII characters in a URI.

You can have them in an IRI, which is what this is:

http://translate.google.com/translate_tts?q=आलू

Browsers typically support IRIs (with some limitations), but many other tools don't (including, apparently, the Android media player). For those tools, you have to convert the IRI to a URI. This is done by:

  • taking any non-ASCII characters in the hostname part of the address and encoding them using the IDN algorithm;

  • taking any non-ASCII characters in other parts of the address (like here, the query) and %-encoding their UTF-8 byte representation.

This gives you:

http://translate.google.com/translate_tts?q=%e0%a4%86%e0%a4%b2%e0%a5%82

which should work anywhere. (And paste a URI like this into a browser and typically it'll display it in IRI form with the Hindi in the address bar.)

bobince
  • 528,062
  • 107
  • 651
  • 834
  • If you try to MediaPlayer.setDataSource() using the encoded uri with %-encoding, it still doesn't play that MP3. But if you paste it into Chrome or Firefox it converts it like you said and plays it. ??? – mellowg Mar 23 '11 at 10:06
  • @mellog: If you have found any solution to playing url with unicode characters in mediaplayer please provide the solution. – gypsicoder Aug 12 '11 at 06:58
  • Sorry didn't find a solution - just gave up – mellowg Aug 13 '11 at 18:37
1

I have found the solution from other person. You have encode the text first. Then you have to add the encode method in "ie" parameter. For your text if you set the url for mediaplayer as http://translate.google.com/translate_tts?ie=UTF-8&q=%e0%a4%86%e0%a4%b2%e0%a5%82 it will play the desired word.

gypsicoder
  • 5,416
  • 5
  • 24
  • 38