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.
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
[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:
Better solutions welcome!