Service with MediaPlayer doesn't stop when closing app - Android -


i'm developing simple game 3 activities (menu, settings , ranking list) needs 1 background music should play smoothly in background if example user leaves menu , goes settings , back.

for created service works perfectly. there 1 major problem: when app closed (user press home button example), music doesn't stop playing.

i have tried ondestroy, onstop, onpause problem not solved.

service:

package com.android.migame;  import android.app.service; import android.content.intent; import android.media.mediaplayer; import android.media.mediaplayer.oncompletionlistener; import android.os.ibinder;  public class meni_music extends service implements oncompletionlistener {      private static final string tag = null;     mediaplayer player;      public ibinder onbind(intent arg0) {         return null;     }      @override     public void oncreate() {         super.oncreate();         player = mediaplayer.create(this, r.raw.menu);         player.setlooping(true); // set looping     }      public int onstartcommand(intent intent, int flags, int startid) {         player.start();         return 1;     }      public ibinder onunbind(intent arg0) {         // todo auto-generated method stub         return null;     }      @override     public void ondestroy() {         player.stop();         player.release();         stopself();     }      @override     public void onlowmemory() {     }      @override     public void oncompletion(mediaplayer mediaplayer) {     } } 

menu:

package com.android.migame;      import android.app.activity;     import android.app.activitymanager;      import android.content.context;     import android.content.intent;     import android.os.bundle;     import android.util.log;     import android.view.view;     import android.view.window;     import android.view.windowmanager;     import android.widget.imageview;      import java.io.bufferedreader;     import java.io.file;     import java.io.filereader;      public class meni extends activity {          @override         public void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);              requestwindowfeature(window.feature_no_title);             getwindow().setflags(windowmanager.layoutparams.flag_keep_screen_on,                     windowmanager.layoutparams.flag_keep_screen_on);             setcontentview(r.layout.meni);              startservice(new intent(meni.this,meni_music.class));          }          @override         protected void onpause() {             super.onpause();         }          @override         protected void onresume() {             super.onresume();         }  } 

easy solutions:

  1. just use 1 activity! use fragments each screen displaying.

  2. use static counter. increment counter when call startactivity(). decrement counter onpause() of activities. when activity pauses, , counter 0, stop music.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -