Reputation: 147
I have a folder called "raw" in my Res folder with a selection of short audio tracks and the screen displays several buttons.
I would like to be able to start the MediaPlayer like this...
String[] track = {"track1", "track2", "track3", etc...}
mPlayer = MediaPlayer.create(this, R.raw.track[x]);
The issue is with the R.raw.track[x]
.
Is there a way to do this in the way I'm attempting or do I need a whole new approach?
Thanks in advance for any advice you can offer.
Upvotes: 2
Views: 2507
Reputation: 1007658
I am going to assume here that your x
is an index, perhaps pulled from a user selection from track
.
You have two main options:
Have a corresponding int[] {R.raw.track1, R.raw.track2, ...}
and look up the R.raw
value that way
Use getResources().getIdentifier()
to look up an identifier at runtime
The second option is easier but uses reflection and so is not super-quick. In your case, that should not matter, as I doubt that you are looking up these IDs in a tight loop.
Upvotes: 2