android - Playing sounds in an array with the next and back button -
i have 3 sounds , trying play them when button pressed. have 2 buttons, next , button. next button move array of sounds in forward direction , play them when clicked, , button play sounds , move array in reverse direction.
here logic trying use:
private int[] sounds = { r.raw.chase, r.raw.get_down, r.raw.under_arrest }; private int pool = 0; mediaplayer mp; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.learn_main); button btnnext = (button) findviewbyid(r.id.button2); button btnback = (button) findviewbyid(r.id.button1); mp = mediaplayer.create(this, sounds[pool]); // initializing on btnnext.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { if (pool < sounds.length - 1) { pool++; } else { pool = 0; } mp.??????????? }); btnback.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { if (pool > 0) { pool--; } else { pool = sounds.length - 1; } mp.??????????? } }); } }
i hope explained wanted, at-least tried. please let me know if failed in clarifying question, edit matter.
you need re-initialise mediaplayer
every time want play different result. here's example:
btnnext.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { if (pool < sounds.length - 1) { pool++; } else { pool = 0; } refreshmediaplayerwithsound(pool); }); btnback.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { if (pool > 0) { pool--; } else { pool = sounds.length - 1; } refreshmediaplayerwithsound(pool); }); private void refreshmediaplayerwithsound(int pool) { if (mp != null) { mp.release(); // release played / held resources } mp = mediaplayer.create(youractivity.this, sounds[pool]); mp.start(); }
Comments
Post a Comment