java - Android MediaPlayer not stopping by clicking another button -
when click on 2 buttons hear 2 sounds. how can stop first sound , play second sound or when play first sound, second sound stops? how can create this? here media player
final linearlayout lm = (linearlayout) findviewbyid(r.id.linearmain); linearlayout.layoutparams params = new linearlayout.layoutparams( layoutparams.wrap_content, layoutparams.wrap_content); jsonarray songs = (jsonarray) response.getjsonarray("songs"); ( int = 0; < songs.length(); i++) { // create linearlayout audio = songs.getjsonobject(i).getstring(tag_audio) ; string chanson = songs.getjsonobject(i).getstring("title") ; string duration = songs.getjsonobject(i).getstring(tag_duration) ; linearlayout ll = new linearlayout(getapplicationcontext()); ll.setorientation(linearlayout.horizontal); final textview txtchanson = new textview(getapplicationcontext()); txtchanson.settext(chanson); txtchanson.setpadding(10, 0, 10, 0); textview txtduration = new textview(getapplicationcontext()); txtduration.settext(duration); final button btn = new button(getapplicationcontext()); final mediaplayer mp = new mediaplayer(); mp.setaudiosessionid(i+1); mp.setdatasource(audio); mp.prepare(); btn.setid(i+1); btn.settypeface(ionicons); btn.settext(getstring(r.string.play_str)); btn.setlayoutparams(params); final int index = i; mp.setoncompletionlistener(new oncompletionlistener() { public void oncompletion(mediaplayer mp) { btn.settext(getstring(r.string.play_str)); } }); btn.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { log.i("tag", "index :" + index); toast.maketext(getapplicationcontext(), "clicked button index :" + btn.getid(), toast.length_long).show(); boolean b = false ; if ( b == false ) { //txtchanson.settextcolor(r.color.green); mp.start(); btn.settext(getstring(r.string.pause_str)); b = true; } else //if (btn.gettext().equals( getstring(r.string.pause_str))) { mp.pause(); btn.settext(getstring(r.string.play_str)); //txtchanson.settextcolor(r.color.gray); b = false; } /* else if (b == true ) { btn.settext(getstring(r.string.play_str)); mp.pause(); }*/ } }); ll.addview(btn); ll.addview(txtchanson); ll.addview(txtduration); lm.addview(ll); }
you should place boolean b = false ;
line out of onclick method should global variable, because on every click, when go onclick method, b
variable gets false value , never steps else clause of if-else statement. else okay.
Comments
Post a Comment