title: Android Service to play music and stop on Pause and on Stop
body: <p>I am implementing MediaPlayer class in my app and I want music played on background, so I used Service instead of Activity or BroadcastReciever. Now the problem is, if user exist or pause the app, music playing on without stopping. I did add onStop and onPause methods, but the problem keeps. And I can't call super.onPause() method like I do in Activity class.</p>
  
  <p>After some research I found out service keeps active even app pauses or stops.(I don't know this is true) but with my experience I think this is true. What should I do? I searched google and stackoverflow for an answer but couldn't get an answer.</p>
  
  <p>Here is a code.</p>
  
  <pre><code> package in.isuru.animation;
  
   import android.app.Service;
   import android.content.Intent;
   import android.media.MediaPlayer;
   import android.os.IBinder;
   import android.util.Log;
  
   public class PlayMusic extends Service{
  
  private static final String TAG = null;
  MediaPlayer player;
  
  public void onCreate(){
      super.onCreate();
      Log.d(TAG, "Music service started onCreate()");
      player = MediaPlayer.create(this, R.raw.oh_holy_night);
      player.setLooping(true);
      //player.setVolume(100, 100);
  }
  
  public int onStartCommand(Intent intent, int flags, int startId){
      player.start();
      Log.d(TAG, "Player started!");
      if(player.isLooping() != true){
          Log.d(TAG, "Player not playing any file.");
      }
      return 1;
  }
  
  public void onStart(Intent intent, int startId){
  
  }
  
  public IBinder onUnBinder(Intent arg0){
      return null;
  }
  
  public void onStop(){
      player.stop();
      player.release();
  }
  
  public void onPause(){
  
      player.stop();
      player.release();
  
  }
  
  public void onDestroy(){
      player.stop();
      player.release();
      Log.d(TAG, "Player destroyed");
  }
  
  public void onLowMemory(){
      player.release();
  }
  
  @Override
  public IBinder onBind(Intent arg0) {
  
      return null;
  }
  
  }
  </code></pre>