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?

有帮助吗?

解决方案

You can do it like this:

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

其他提示

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

 Uri uri =Uri.parse("android.resource://" + getPackageName() + "/raw/" + imageName);// without extention
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top