stream - How to play and pause in only one button - Android -
i have 2 buttons in media player streams radio station, play , pause. want make 1 button has 2 function. first click want play , second click want pause it. , when click again want play it. need help. here code.
play = (button) findviewbyid(r.id.play); pause = (button) findviewbyid(r.id.pause); play.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { play(); } }); play.performclick(); pause.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { pause(); } }); } private void play() { uri myuri = uri.parse("shoutcast url"); try { if (mp == null) { this.mp = new mediaplayer(); } else { mp.stop(); mp.reset(); } mp.setdatasource(this, myuri); // go initialized state mp.setaudiostreamtype(audiomanager.stream_music); mp.setonpreparedlistener(this); mp.setonbufferingupdatelistener(this); mp.setonerrorlistener(this); mp.prepareasync(); log.d(tag, "loadclip done"); } catch (throwable t) { log.d(tag, t.tostring()); } } @override public void onprepared(mediaplayer mp) { log.d(tag, "stream prepared"); mp.start(); } private void pause() { mp.pause(); } @override public void ondestroy() { super.ondestroy(); stop(); } public void oncompletion(mediaplayer mp) { stop(); }
create 1 button , add buttonstatus. can check status in listener.
example:
boolean isplaying = false; playpause = (button) findviewbyid(r.id.play); playpause.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { if (isplaying) { pause(); }else{ play(); } isplaying = !isplaying; } });
Comments
Post a Comment