Player getting null after Home button pressed Android -


i creating streaming player , far working fine. can manage start , stop streaming. when press home button, activity closed sound still playing. fine that, problem when start application again , hit stop button, object null. thats code:

public class main extends activity {  private mediaplayer player;  /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main);  }  public void onplayclick(view v) {      try {         player = new mediaplayer();         player.setdatasource("*****");         player.setaudiostreamtype(audiomanager.stream_music);                    player.setonpreparedlistener(new onpreparedlistener() {             @override             public void onprepared(mediaplayer mp) {                 log.i("in", "prepared listener");                 player.start();             }         });         player.prepareasync();     } catch (illegalargumentexception e) {         e.printstacktrace();     } catch (illegalstateexception e) {         e.printstacktrace();     } catch (ioexception e) {         e.printstacktrace();     }  }  public void onstopclick(view v) {      if (player != null && player.isplaying()) {         player.stop();         player.release();         player = null;     } } 

}

does know wrong simple code? many t

i think worth noting if going go activity route opposed service, should put

if (player != null && player.isplaying()) {     player.stop();     player.release();     player = null; } 

inside onstop(), or ondestroy() method of activity also. if put in onstop() audio stop when home button pressed(or anytime activity takes on phone rings perhaps). if put in ondestroy() keep playing until system needs resources application using. either way idea app not holding on the mediaplayer resources incase user doesn't hit stop button whatever reason.


Comments