Question

For example, I have a string "R.raw.tch_swan_lake". I want to play one of many files in the raw folder, which match to my string:

melody = MediaPlayer.create(this, R.raw.tch_swan_lake);

How to use a string as a resource pointer?

Was it helpful?

Solution

You can do it like this:

    int resId = context.getResources().getIdentifier("tch_swan_lake", "raw", context.getPackageName()); 
    melody = MediaPlayer.create(this, resId);

OTHER TIPS

This is how i get play able resource from 'raw;

 Uri uri =Uri.parse("android.resource://" + getPackageName() + "/raw/" + imageName);// without extention
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top