How to code play stop button on android studio -




i need advice code below. i'm trying create simple apps when people click play button, music start playing, , when people click stop button, music stop playing. i'm using single button 2 task start , stop.

i test on android simulator, start , stop button working wanted to. problem occurred when build apk file , play on device. stop button cannot work wanted to. if press stop button, music restart playing beginning.

any ideas problem? thank you.

btn_playstop.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             if (btn_playstop.ispressed()) {                 if (player.isplaying()) {                     player.stop();                     try {                         player.prepare();                     } catch (illegalstateexception e) {                         e.printstacktrace();                     } catch (ioexception e) {                         e.printstacktrace();                     }                     player.seekto(0);                     btn_playstop.setbackgroundresource(r.drawable.play);                 }                 else {                     player.start();                     btn_playstop.setbackgroundresource(r.drawable.stop);                 }             }         }     }); 

to achieve play/stop button, way.

btn_playstop.setonclicklistener(new view.onclicklistener() {     @override     public void onclick(view v) {         if (player != null && player.isplaying()) {             player.stop();             player.release();             player = null;               try {                  player.prepare();               } catch (illegalstateexception e) {                 // todo auto-generated catch block               e.printstacktrace();               } catch (ioexception e) {                 // todo auto-generated catch block               e.printstacktrace();               }               player.seekto(0);  //<---- resume player specified position. helpful pause button. can remove stop button.               btn_playstop.setbackgroundresource(r.drawable.play);           }         else {                           player.start();                 btn_playstop.setbackgroundresource(r.drawable.stop);         }      }     }); 

you don't have call seekto(length) method stop button meant resuming player specified position. if (btn_playstop.ispressed()) unnecessary have called button using setonclicklistener.





wiki

Comments

Popular posts from this blog

elasticsearch - what is the equivalent data type for geo_point in hibernate search? -

Jenkins: find build number for git commit -

firebase - How to wait value in Ionic 2 -