title: Android mediaplayer singleton service won't stop playing
body: <p>I need to have background music in all my activities. It should stop when the application is not foreground. As I'm developing for 2.3 I can't use the ActivityLifeCycleCallBacks class. I implemented the solution at <a href="https://stackoverflow.com/questions/3667022/android-is-application-running-in-background/5862048#5862048">Checking if an Android application is running in the background</a> and then decided to make the mediaplayer a singleton and use it in a service. </p>
  
  <p>Everything works fine and if I press home, select quit from the menu or I make the application go background any way the sound stops but... <strong>after some random time when I'm doing something else or even when the screen is turned off the music will start again out of the blue. Even if I kill the application from task manager the will start again later again.</strong> </p>
  
  <p>This is my first singleton and my first time playing with service so I guess I'm missing something really basic. I think I'm closing the service but apparently I'm not.
  Here is the code:</p>
  
  <p>PlayAudio.java</p>
  
  <pre><code>import ...
  public class PlayAudio extends Service{
      private static final Intent Intent = null;
      MediaPlayer objPlayer;
      private int length = 0;
      boolean mIsPlayerRelease = true;
      private static PlayAudio uniqueIstance; //the singleton
      static PlayAudio mService;
      static boolean mBound = false; // boolean to check if the service containing this singleton is binded to some activity
      public static boolean activityVisible; // boolean to check if the activity using the player is foreground or not
  
  //My attempt to make a singleton
  public static PlayAudio getUniqueIstance(){
      if (uniqueIstance == null) {
          uniqueIstance = new PlayAudio();
      }
      return uniqueIstance;
  }
  public static boolean isActivityVisible() {
      return activityVisible;
    }  
  
    public static void activityResumed() {
      activityVisible = true;
    }
  
    public static void activityPaused() {
      activityVisible = false;
    }
  static public ServiceConnection mConnection = new ServiceConnection() {// helper for the activity
  
      public void onServiceConnected(ComponentName className,
              IBinder service) {
          LocalBinder binder = (LocalBinder) service;
          mService = binder.getService();
          mBound = true;
      }
  
      public void onServiceDisconnected(ComponentName arg0) {
          mBound = false;
      }
  };
  public static Intent createIntent (Context context) { //helper for the activity using the player
      Intent intent = new Intent(context, PlayAudio.class);
      return intent;
  }
  private final IBinder mBinder = new LocalBinder();
  
  public class LocalBinder extends Binder {
      PlayAudio getService() {
          // Return this instance so clients can call public methods
          return PlayAudio.this;
      }
  }
  @Override
  public IBinder onBind(Intent intent) {
      return mBinder;
  }
  public void onCreate(){
      super.onCreate();
      Log.d(LOGCAT, "Service Started!");
      objPlayer = MediaPlayer.create(this,R.raw.kickstarterreduced);
      objPlayer.setLooping(true);
      mIsPlayerRelease = false;
  }
  
  public int onStartCommand(Intent intent, int flags, int startId){
      objPlayer.start();
      Log.d(LOGCAT, "Media Player started!");
      if(objPlayer.isLooping() != true){
          Log.d(LOGCAT, "Problem in Playing Audio");
      }
      return 1;
  }
  
  public void onStop(){
      objPlayer.setLooping(false);
      objPlayer.stop();
      objPlayer.release();
      mIsPlayerRelease = true;
  }
  
  public void onPause(){
      if(objPlayer.isPlaying())
      {
          objPlayer.pause();
          length=objPlayer.getCurrentPosition(); // save the position in order to be able to resume from here
      }   
  }
  public void resumeMusic() // if length is 0 the player just start from zero
  {   if (mIsPlayerRelease == true) {
      objPlayer = MediaPlayer.create(this,R.raw.kickstarterreduced);
      mIsPlayerRelease = false;
  }
      if(objPlayer.isPlaying()==false )
      {
          if (length != 0) objPlayer.seekTo(length);
          objPlayer.start();
      }
  }
  
  }
  </code></pre>
  
  <p>And this are the methods I have implemented in every activity's class</p>
  
  <pre><code>SharedPreferences sharedPrefs;
  PlayAudio playerIstanced;
  public static boolean activityVisible;
  @Override
  public void onStart() {
      super.onStart();
      sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); 
     }
  @Override
  public void onResume() {
         super.onResume();
         playerIstanced= PlayAudio.getUniqueIstance(); //call singleton
         bindService(PlayAudio.createIntent(this), playerIstanced.mConnection, Context.BIND_AUTO_CREATE); // create the service
         if (sharedPrefs.getBoolean("sound", true) == true) {// if sound is enabled in option it will start the service
             startService(PlayAudio.createIntent(this));
             playerIstanced.mService.activityResumed();
             if (playerIstanced.mBound == true) {
             playerIstanced.mService.resumeMusic();
             }
         }
  
     }
     @Override
     public void onPause() {
         super.onPause();
         playerIstanced.mService.activityPaused();
         final Handler handler = new Handler();
         handler.postDelayed(new Runnable() {
           public void run() {
             //If the phone lags when changing activity (between onPause() and the other activity onResume() the music won't stop. If after 500ms onResume() is not called it means the activity went background...Am I messing with service here?
               if (playerIstanced.mService.isActivityVisible() != true) {
                   playerIstanced.mService.onPause();
               }
           }
         }, 500);
  
  }
     @Override
     public void onStop(){
         super.onStop();
         // Unbind from the service
         if (playerIstanced.mService.mBound) {
             playerIstanced.mService.mBound = false;
             unbindService(playerIstanced.mService.mConnection);
  
         }
     }
  
  
  
  }
  </code></pre>