Friday, 6 September 2013

Random audio button stops working after set amount of clicks

Random audio button stops working after set amount of clicks

I'm having a bit of a problem, basically I've currently got a single
button setup to play a variety of sounds, on each click a random sound
should play. That works fine however after a certain amount of clicks of
the button which equals the amount of sound files I have it playing, it
stops playing anymore. Pretty stumped as to why it's happening and a way
of getting around it.
private final int SOUND_CLIPS = 11;
private int mfile[] = new int[SOUND_CLIPS];
private Random rnd = new Random();
MediaPlayer mpButtonOne;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//I list all my sounds files here, removed for sake of saving space.
Button bButtonOne = (Button) findViewById(R.id.button1);
bButtonOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mpButtonOne = MediaPlayer.create(myMenu.this,
mfile[rnd.nextInt(SOUND_CLIPS)]);
mpButtonOne.seekTo(0);
mpButtonOne.start();
}
});
}
}
All of this code does what I want it to do which is play a random sound
clip out of a list of clips on every button click however it seems to only
do it a set amount of times and then stops playing anything. Any help
would be appreciated.

No comments:

Post a Comment