android - why is media player starts more than once and media player won't stop in button click which started at on create? -


i need add background music android application. have declared mediaplayer object class variable , create inside oncreate(). have start mediplayer object inside oncreate(). have stop media player in button click method starts new intent. requirement play music when starts layout , stop when starts next layout, starts more once , won't stop. here code.

 mediaplayer  startmusic;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_cover);         setrequestedorientation(activityinfo.screen_orientation_landscape);         startmusic= mediaplayer.create(cover.this, r.raw.startgame);          if(!(startmusic.isplaying()))         {             startmusic.start();         }     }    public  void  btnstartclick(view v)     {         startmusic.pause();         intent intent = new intent(this,scaleexercise.class);         startactivity(intent);     } 

can please tell me whats wrong here(asap). thankyou

try this

 static  mediaplayer  startmusic; @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_cover);     setrequestedorientation(activityinfo.screen_orientation_landscape);     startmusic= mediaplayer.create(cover.this, r.raw.startgame);   }   @override protected void onstart() {     // todo auto-generated method stub      if(!(startmusic.isplaying()))     {            startmusic.setlooping(true);         startmusic.start();     }      super.onstart();  }   @override protected void onstop() {     // todo auto-generated method stub     startmusic.stop();     super.onstop();  } public  void  btnstartclick(view v) {      intent intent = new intent(this,scaleexercise.class);     startactivity(intent); } 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -