I want to my application play random sound when user click on button. So I don't want android natural sound, I want to play my own sound every time user click on one button. Does anyone knows the solution of this problem.
To play a sound:
Play sound on button click android
For a random sound you just need to add all sounds in a list. And in the onClickListener just get a random sound from your list. Something like this:
List<Integer> soundList = new ArrayList<Integer>();
soundList.add(R.raw.sound1);
soundList.add(R.raw.sound2);
soundList.add(R.raw.sound3);
soundList.add(R.raw.sound4);
myButton.setOnClickListener(new OnClickListener {
@Override
public void onClick(View v) {
playRandomSound();
}
});
private void playRandomSound() {
int randomInt = (new Random().nextInt(soundList.size()));
int sound = soundList.get(randomInt);
MediaPlayer mp = MediaPlayer.create(this, sound);
mp.start();
}
No guarantee that this works! It's just an example, how you could do it!