I am fairly new to Android Application development, and I'm trying to play a random .mp3 from the /res/raw folder.
FIXED I have this so far, but I came across a FileNotFoundException.
FIXED Only plays a random sound on first click, after that it is the same sound unless reopen the app.
NEW ISSUE Now plays random sounds, but when I click the button multiple times the sounds start playing at the same time and still getting the "start() mUri is null" message in the logs.
UPDATED CODE
MediaPlayer player;
int soundIndex;
AssetFileDescriptor descriptor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* gets a random index from an array of sounds
*
* @return
*/
public int getRandomSoundIndex(){
int soundIndex;
int[] sound = SOUNDZ;
Random random = new Random();
soundIndex = random.nextInt(sound.length);
return sound[soundIndex];
}
/**
* Plays that random sound on button click
* @param button
*/
public void playRandomSound(View button){
//where button is physically located
button = (Button) findViewById(R.id.button1);
//get random sound index
soundIndex = getRandomSoundIndex();
//make media player
player = MediaPlayer.create(this, soundIndex);
//play sound
player.start();
}
Here is the log:
09-21 17:42:32.528: D/MediaPlayer(4282): start() mUri is null