media player - Android playing sound on button click -
i'm trying play multiple times 2 different .wav files on button click. want play sound1 sound2 sound1 , on.. here fragment of code.
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_box); mp = mediaplayer.create(box.this, r.drawable.sound1); mp2 = mediaplayer.create(box.this, r.drawable.sound2); handler = new handler(); sbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { rounds = new integer(et.gettext().tostring()); for(int j = 0; j < rounds; j ++) { f(); g(); } } }); } public void f() { cdt1 = new countdowntimer(5000, 1000) { public void ontick(long millisuntilfinished) { } public void onfinish() { mp.setlooping(true); mp.setvolume(1.0f, 1.0f); mp.start(); }}.start(); } public void g() { cdt = new countdowntimer(7000, 1000) { public void ontick(long millisuntilfinished) { } public void onfinish() { mp2.setlooping(true); mp2.setvolume(1.0f, 1.0f); mp2.start(); }}.start(); }
shall reset them every time play sound ? in advance.
you can check mediaplayer if still playing like:
if (mp.isplaying()) { //do nothing } else { //start new sound }