android - Music(activity) restarts when rotating device...why does it happen?and how can i stop it from restarting? -


well think question describes , anyways here code

public class  nowplaying extends activity implements serializable  {     static mediaplayer mp =new mediaplayer();      string artist;progressdialog pd;     int position; string key=null ;     /*int z=0;     seekbar songprogressbar ;     textview songcurrentdurationlabel;     textview songtotaldurationlabel ;     private handler mhandler = new handler();     private utilities utils=new utilities();     */      @suppresslint("newapi")     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.now_playing);         intent = getintent();          position=i.getintextra("data2", 0);          /* songprogressbar = (seekbar) findviewbyid(r.id.songprogressbar);           songcurrentdurationlabel = (textview) findviewbyid(r.id.songcurrentdurationlabel);           songtotaldurationlabel = (textview) findviewbyid(r.id.songtotaldurationlabel);             */          final arraylist<songdetails> songs = getintent().getparcelablearraylistextra("data1");            pd = new progressdialog(this);          pd.setmessage("loading...");               playservice(songs,position  );           button bfake=(button) findviewbyid(r.id.bfake);         linearlayout ll=(linearlayout) findviewbyid(r.id.ll);         button pause=(button)findviewbyid(r.id.bplaypause);         // songprogressbar.setonseekbarchangelistener((onseekbarchangelistener) this); // important          mp.setoncompletionlistener(new mediaplayer.oncompletionlistener() {             @override             public void oncompletion(mediaplayer player) {                  position=position+1;                     playservice(songs,position);                  }});          bfake.setontouchlistener(new onswipetouchlistener()          {           public void onswiperight() {                  position=position-1;                     playservice(songs,position  );              }                 public void onswipeleft() {                  position=position+1;                     playservice(songs,position );                 }                 public void onswipebottom() {                     mp.stop();                 }                });             ll.setontouchlistener(new onswipetouchlistener() {                public void onswipebottom() {                     mp.stop();                 }                });               pause.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v)              {if(mp.isplaying())                 mp.pause();             else                 mp.start();             }             });     }       private void playservice( arraylist<songdetails> songs, int position    )           {             int currentposition= 0;             int total = mp.getduration();            /* while (mp!=null && currentposition<total) {                 try {                     thread.sleep(1000);                     currentposition= mp.getcurrentposition();                 } catch (interruptedexception e) {                     break;                 } catch (exception e) {                     break;                 }                       //      songprogressbar.setprogress(currentposition);             */                try {                  string ab=songs.get(position).getpath2().tostring();                  artist=songs.get(position).getartist();                  new xmlparse().execute();                  if(mp.isplaying())                     {   mp.stop();                         }                     mp.reset();                     mp.setdatasource(ab) ;                     mp.prepare();                     mp.start();             //      songprogressbar.setprogress(0);                 //  songprogressbar.setmax(mp.getduration());                     new thread().start();                 } catch (illegalargumentexception e) {                      e.printstacktrace();                 } catch (securityexception e) {                     e.printstacktrace();                 } catch (illegalstateexception e) {                     e.printstacktrace();                 } catch (ioexception e)                 {                     e.printstacktrace();                 }             }      /*public void onstarttrackingtouch(seekbar seekbar) {         }          public void onstoptrackingtouch(seekbar seekbar) {         }          public void onprogresschanged(seekbar seekbar, int progress,                 boolean fromuser) {             if(fromuser) mp.seekto(progress);          }   */ class xmlparse extends asynctask<void,drawable,drawable>  {     protected void onpostexecute(drawable result) {        // super.onpostexecute(result);         pd.dismiss();         linearlayout ll=(linearlayout) findviewbyid(r.id.ll);          ll.setbackgrounddrawable(result);           // toast.maketext(getapplicationcontext(),result, 1000).show();     }      @override     protected void onpreexecute() {         super.onpreexecute();         pd.show();     }     @override     protected drawable doinbackground(void... params) {         drawable s =getdata();         return s;     }        public drawable getdata()     {  drawable drawable = null;//=new bitmapdrawable(bmp);           artist=artist.trim();         artist=artist.replace(" ", "%20");          try{            url url = new url("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=" +artist+              "&api_key=1732077d6772048ccc671c754061cb18");         urlconnection connection = url.openconnection();         documentbuilderfactory dbf = documentbuilderfactory.newinstance();         documentbuilder db = dbf.newdocumentbuilder();          final document document = db.parse(connection.getinputstream());         document.getdocumentelement().normalize();         xpathfactory xpathfactory = xpathfactory.newinstance();         xpath xpathevaluator = xpathfactory.newxpath();         xpathexpression nameexpr = xpathevaluator.compile("//lfm/artist/image");         // xpathexpression nameexpr = xpathevaluator.compile("//lfm/tracks/track/image");         nodelist nl = (nodelist) nameexpr.evaluate(document, xpathconstants.nodeset);           (int zzz = 0; zzz < nl.getlength(); zzz++)         {             node currentitem = nl.item(zzz);             key = currentitem.gettextcontent();               // key = currentitem.getattributes().getnameditem("uri").getnodevalue();         }url ulrn = new url(key);         httpurlconnection con = (httpurlconnection)ulrn.openconnection();         inputstream = con.getinputstream();         bitmap bmp = bitmapfactory.decodestream(is);         drawable=new bitmapdrawable(bmp);          }         catch(exception e)         {}         return drawable;}      }} 

it restarts because activity recreated... each time user change orientation of device oncreate method called again. fix try add activity in manifest file:

   android:configchanges="orientation|keyboardhidden"> 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -