I've got a button that on press it plays a random sound clip followed by another and then another via one mediaplayer however after pressing the button numerous times (15-20 times) all audio just stops. I'm releasing the mediaplayer after the last audio clip is played so I don't see that being the reason. Any pointers?
public class Main extends Activity {
MediaPlayer mp;
Button generate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
generate = (Button) findViewById(R.id.sound);
}
public void youSir (View view)
{
generate.setVisibility(View.INVISIBLE);
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(3) + 1;
switch (randomInt){
case 1: mp = MediaPlayer.create(this, R.raw.beg); mp.start();
break;
case 2: mp = MediaPlayer.create(this, R.raw.begone); mp.start();
break;
case 3: mp = MediaPlayer.create(this, R.raw.begtwo); mp.start();
break;
}
mp.setOnCompletionListener(new OnCompletionListener(){
// @Override
public void onCompletion(MediaPlayer arg0) {
audioTwo();
}
});
}
protected void audioTwo() {
// TODO Auto-generated method stub
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(3) + 1;
switch (randomInt){
case 1: mp = MediaPlayer.create(this, R.raw.start); mp.start();
break;
case 2: mp = MediaPlayer.create(this, R.raw.end); mp.start();
break;
case 3: mp = MediaPlayer.create(this, R.raw.mid); mp.start();
break;
}
mp.setOnCompletionListener(new OnCompletionListener(){
// @Override
public void onCompletion(MediaPlayer arg0) {
audioThree();
}
});
}
protected void audioThree() {
// TODO Auto-generated method stub
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(3) + 1;
switch (randomInt){
case 1: mp = MediaPlayer.create(this, R.raw.three); mp.start();
break;
case 2: mp = MediaPlayer.create(this, R.raw.two); mp.start();
break;
case 3: mp = MediaPlayer.create(this, R.raw.one); mp.start();
break;
mp.setOnCompletionListener(new OnCompletionListener(){
// @Override
public void onCompletion(MediaPlayer arg0) {
mp.stop();
mp.release();
generate.setVisibility(View.VISIBLE);
}
});
}