Issue
I want to select certain audio files when a user presses a button but i cant seem to find how i can change the R.raw. so that it will change in here.
I know there should be a easier sollution than creating 1000 if else with R.raw.0, R.raw.1 ,...
So in my example i have selected file "c.mp3" but this must be eventualy change to 000 till 499. So random (or selective) say 050 => this shoud become R.raw.050
}
@Override
public void play() {
final MediaPlayer mp = MediaPlayer.create(context, R.raw.c);
mp.start();
}
Solution
Let's say that you have your random number like this :
String rand = "050";
It is easy you need just to find the raw resource id and pass it to your MediaPlayer.create method like that :
int rawResourceId = context.getResources().getIdentifier(rand, "raw", this.getPackageName());
final MediaPlayer mp = MediaPlayer.create(context, rawResourceId);
mp.start();
Suggestion :
- It's better to rename your resources files by prefixing them by a letter like f_000 , f_001, ...., f_050.
Answered By - Youssef Najeh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.