Pregunta

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?

¿Fue útil?

Solución

You can do it like this:

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

Otros consejos

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

 Uri uri =Uri.parse("android.resource://" + getPackageName() + "/raw/" + imageName);// without extention
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top