Reputation: 36
I wrote a method for Android which takes the id of a button and compares it to the name of a sound resource and sets the media player to that resource. It should play the sound but fails to. For your reference, the id of the button is the same name as the sound file. Any thoughts on why this is not playing? Here is the method.
public void buttonTapped(View view){
int id = view.getId();
String ourId = "";
ourId = view.getResources().getResourceEntryName(id);
int resourceId = getResources().getIdentifier(ourId, "raw", "com.test.basicphrases");
MediaPlayer mediaPlayer = MediaPlayer.create(this, resourceId);
mediaPlayer.start();
}
Upvotes: 0
Views: 335
Reputation:
private int[] songs = { R.raw.song1, R.raw.song2};
public void buttonTapped(View view){
int id = view.getId();
String ourId = "";
ourId = view.getResources().getResourceEntryName(id);
int resourceId = songs[id];
MediaPlayer mediaPlayer = MediaPlayer.create(this, resourceId);
mediaPlayer.start();
}
This should work. It isn't like your method, as it stores all songs in an array in the java class.
Upvotes: 1