Md Aman
Md Aman

Reputation: 340

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 :

 java.lang.IllegalArgumentException

The above exception occurs when we have path of music files is in Url Encoded form (i.e,path : storage/sdcard/song%20music%20file.mp3) the path is of phone storage and not the url of song on internet.

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.

Upvotes: 1

Views: 109

Answers (1)

Aman Ansari
Aman Ansari

Reputation: 86

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

Upvotes: 1

Related Questions