Android:MediaPlayer 无法加载包含印地语字符的 URL
当我运行此代码时抛出异常。如果将 URL 中的印地语字符替换为“Hello”,则可以正常播放该文件。
当我在浏览器中加载此 URL(带有印地语字符)时,它播放得很好。
这是怎么回事?
这是我的代码:
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(getResources().getString(R.string.test));
mediaPlayer.prepare();
这是字符串资源定义:
<string name="test">http://translate.google.com/translate_tts?q=आलू</string>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不认为 unicode 字符在 URL 中是合法的,除非你对它们进行编码。这是规格:
https://www.rfc-editor.org/rfc/rfc1738
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 是对的,URI 中不能包含非 ASCII 字符。
您可以将它们放在IRI中,如下所示:
浏览器通常支持 IRI(有一些限制),但许多其他工具不支持(显然包括 Android 媒体播放器)。对于这些工具,您必须将 IRI 转换为 URI。这是通过以下方式完成的:
获取地址主机名部分中的任何非 ASCII 字符,并使用 IDN 算法;
在地址的其他部分(例如这里的查询)中获取任何非 ASCII 字符,并对它们的 UTF-8 字节表示进行 % 编码。
这给了你:
应该在任何地方都可以工作。 (将这样的 URI 粘贴到浏览器中,通常它会以 IRI 形式显示,并在地址栏中显示印地语。)
+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:
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:
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.)
我已经从其他人那里找到了解决方案。您首先对文本进行了编码。然后你必须在“ie”参数中添加编码方法。对于您的文本,如果您将媒体播放器的网址设置为 http://translate.google.com/translate_tts?ie=UTF-8&q=%e0%a4%86%e0%a4%b2%e0%a5%82< /a> 它将播放所需的单词。
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.