1

I am trying to play an mp3 using the Media player class. When I hardcode the mp3 resource it works, however I would like the resource to come from a string instead of directly accessing it like R.raw.mp3name.

Bundle bundle = getIntent().getExtras();
String Choice= bundle.getString("bundledChoice");        
Uri myUri = Uri.parse("R.raw." + Choice);        
mPlayer = MediaPlayer.create(activity.this, myUri);
mPlayer.start();

If I change the second to last line to mPlayer = MediaPlayer.create(activity.this, R.raw.song) it will work, the problem is creating the resource URI dynamically from the string which is obtained from the bundle.

Please advise.

palacsint
  • 28,416
  • 10
  • 82
  • 109
brux
  • 3,197
  • 10
  • 44
  • 78
  • i suppose a better way of putting it is "How do I reference a Resource @ R.raw... using a string" – brux Jan 12 '12 at 13:08
  • Resource at R.raw is made at the compilation of the R class which creates the IDs for the resources. The URI.parse() requires an RFC 2396-compliant, encoded URI PARAMETER. R.raw.song is the int ID of this song. You might want to read something like this http://stackoverflow.com/questions/559902/android-how-can-i-convert-android-net-uri-object-to-java-net-uri-object and check how to make a Uri from a File object. Thanks – Sergey Benner Jan 12 '12 at 13:45
  • Here http://stackoverflow.com/questions/3004713/get-content-uri-from-file-path-in-android – Sergey Benner Jan 12 '12 at 14:00
  • Thanks Sergey, I used this post to get the answer http://stackoverflow.com/questions/6679434/android-findviewbyid-with-a-variant-string – brux Jan 12 '12 at 18:42
  • @brux, Sorry I'm being dim here, could you post a code fragment? I can't quite make the link between the post you refer to and solving the problem ... – Neil Townsend Sep 12 '12 at 20:59
  • @brux, Ahh, sorted it, I've put another answer up, interested to see if it's what you meant ... – Neil Townsend Sep 13 '12 at 10:13

6 Answers6

5

The solution I am using to this little challenge is based on @Creator's answer to this post: Android - getting an image resource into Uri: IllegalArgumentException.

The solution is to use

Uri uri = Uri.parse("android.resource://[package]/[res type]/[res name]");

[package] you either know (com.company.blah.superdooperthing), or can be determined using getPackageName().

[res type] is (I think, and it seems to work) the directory inside "res" that you are pulling the resource from. So, for an audio file .../res/raw/nicemusic.mp3, res would be "raw".

[res name] is (I think, and it seems to work) the file name (less extension) of the resouce. So, for the example above, it would be "nicemusic".

So, a more systematic approach might be:

String dataResourceDirectory = "raw";
String dataResoruceFilename = "nicemusic";

Uri uri=Uri.parse("android.resource://" + getPackageName() + "/" + 
                  dataResourceDirectory + "/" + dataResoruceFilename);

Better solutions welcome!

Community
  • 1
  • 1
Neil Townsend
  • 6,024
  • 5
  • 35
  • 52
2

Have you tried to do it this way?

MediaPlayer mp = new MediaPlayer();
mp.setDataSource(path+"/"+fileName); 

hope this helps

Sergey Benner
  • 4,421
  • 2
  • 22
  • 29
1

I found this solution which is very simple and works (for me), where everything I tried with Uri.parse() did not work.

private void playMp3(String nameOfFile){
    MediaPlayer mPlayer = MediaPlayer.create(this, getResources().getIdentifier(nameOfFile, "raw", getPackageName()));
    mPlayer.start();
}
Community
  • 1
  • 1
Wil-It
  • 21
  • 6
0

Answer can be found here. Resolving the int ID of the resource helps: Android findViewbyId with a variant string

Community
  • 1
  • 1
brux
  • 3,197
  • 10
  • 44
  • 78
-1

What you are trying to do it to turn the literal stringR.raw.xyz into a URI. What you really want is to first resolve R.war.xyz into a string and then turn that into an Uri. There is e.g. Context.getString() for this purpose.

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
-1

Try this :

Bundle bundle = getIntent().getExtras();
    String choice= blabla// your solution to get song name
    String debug = "R.raw." + choice;   
    Uri myUri = Uri.parse(debug);        
    mPlayer = MediaPlayer.create(activity.this, myUri);
    mPlayer.start();
Alone89
  • 307
  • 1
  • 12