Play sound file one after another in android -
i trying play sound in android 1 after "res/raw" folder. playing first sound sounds array. there other way it?
there 3 sound file in raw folder
1.aa.ogg 2.ma.ogg 3.ar.ogg
package com.protonray.calculatorplus; import java.util.list; import android.media.mediaplayer; import android.media.mediaplayer.oncompletionlistener; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.edittext; import android.app.activity; public class layoutnew extends activity{ int count=0; int[] sounds={r.raw.aa,r.raw.ma,r.raw.ar }; mediaplayer mp=new mediaplayer(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.layout_new); button play=(button) findviewbyid(r.id.play); play.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub count =0 ; //play frist sound mp =mediaplayer.create(layoutnew.this,sounds[count]); mp.start(); count =1 ; } }); mp.setoncompletionlistener(new oncompletionlistener() { @override public void oncompletion(mediaplayer mp) { // todo auto-generated method stub if(count<3){ mp.stop(); //play next sound mp =mediaplayer.create(layoutnew.this,sounds[count]); mp.start(); count++; } } }); } }
have @ setnextmediaplayer. should accomplish need.
simply create 3 media players (one each sound file) in onclick
, set next player first 2 files next file's player, example:
play.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { ( int snd = 0; snd < 3; snd++ ) { mp[snd] = mediaplayer.create( layoutnew.this, sounds[snd] ); } ( int snd = 0; snd < 2; snd++ ) { mp[snd].setnextmediaplayer( mp[snd+1] ); } );
you should release 3 media players when complete (by calling release()
on each of them). done adding oncompletion
handler last media player only (i.e. mp[2]
), cleanup once 3 sounds have completed playing.
edit, since require api 8, can use method currently, have set oncompletionlistener
both first , second sounds, trigger when first 1 finishes, not second one. try code:
mediaplayer mp; play.setonclicklistener( new view.onclicklistener() { @override public void onclick(view arg0) { mp = mediaplayer.create( layoutnew.this, sounds[0] ); mp.start(); mp.setoncompletionlistener( new oncompletionlistener() { @override public void oncompletion(mediaplayer mp) { mp.dispose(); mp = mediaplayer.create( layoutnew.this, sounds[1] ); mp.start(); mp.setoncompletionlistener( new oncompletionlistener() { @override public void oncompletion(mediaplayer mp) { mp.dispose(); mp = mediaplayer.create( layoutnew.this, sounds[2] ); mp.start(); mp.setoncompletionlistener( new oncompletionlistener() { public void oncompletion(mediaplayer mp) { mp.dispose(); mp = null; } }); } }); } }); } }
i did not test code, should work. key thing take away need call setoncompletionlistener
each time create new mediaplayer, new instance notify when completes.
also, because setting in onclick
(all @ once) not need use counter unless want to, or have other reason it. either way, should illustrate sufficiently doing wrong , how works.
Comments
Post a Comment