开发者

Android: MediaPlayer can't load URL with Hindi characters

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 开发者_Go百科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>


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


+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.)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜