简体   繁体   English

为什么在 MediaPlayer static 方法 create() 中使用的 Uri 会抛出 java.lang.IllegalArgumentException?

[英]Why Uri used in MediaPlayer static method create() throws java.lang.IllegalArgumentException?

The problem is that when we use a uri to get the instance of Mediaplayer class using Mediaplayer.create(Context, uri) it throws exception as of below:问题是,当我们使用uri获取Mediaplayer class 的实例时,使用Mediaplayer.create(Context, uri)会引发如下exception

 java.lang.IllegalArgumentException

The above exception occurs when we have path of music files is in Url Encoded form ( ie,path: storage/sdcard/song%20music%20file.mp3 ) the path is of phone storage and not the url of song on internet.当我们的音乐文件路径为 Url 编码形式( ie,path: storage/sdcard/song%20music%20file.mp3 )时,会发生上述异常,该路径是phone storage路径,而not the url

Uri uri = Uri.parse(path);
//the below line creates the problem.
mediaplayer = Mediaplayer.create(context, uri);

while in other cases of path of the files it works like charm why this problem occurs and if there is any way of resolving this problem how can we do this?而在文件路径的其他情况下,它就像魅力一样工作为什么会出现这个问题,如果有任何方法可以解决这个问题,我们该怎么做? Any suggestion,help will be appreciated.任何建议,帮助将不胜感激。

You can use code like below to solve the issue:您可以使用如下代码来解决问题:

Uri myUri = Uri.parse(Uri.encode(path));
//then use myUri like
mediaplayer = Mediaplayer.create(context, myUri);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM