title: Background music Android
body: <p>Okey, this is my problem. I have one service class where Ive managed to create media player to play music in background all time. Here is code:</p>
  
  <pre><code>package com.test.brzoracunanje;
  import android.app.Service;
  import android.content.Intent;
  import android.media.MediaPlayer;
  import android.os.IBinder;
  
  public class BackgroundSoundService extends Service {
  private static final String TAG = null;
  MediaPlayer player;
  public IBinder onBind(Intent arg0) {
  
      return null;
  }
  @Override
  public void onCreate() {
      // TODO Auto-generated method stub
      super.onCreate();
         player = MediaPlayer.create(this, R.raw.test_cbr);
          player.setLooping(true); // Set looping
          player.setVolume(100,100);
          player.start();
  }
  @Override
  public void onStart(Intent intent, int startId) {
      super.onStart(intent, startId);
  }
  public void onDestroy() {
      // TODO Auto-generated method stub
      super.onDestroy();
  }
  
  protected void onNewIntent() {
      player.pause();
  }
  }
  </code></pre>
  
  <p>But now I have problem when I click on <code>HOME</code>, or <code>BACK</code> button. It still plays music. Does anyone knows how to solve that problem?</p>
  
  <p>And here is code how i call this service on class where I want to play music;</p>
  
  <pre><code>  Intent svc=new Intent(this, BackgroundSoundService.class);
      startService(svc);
  </code></pre>