If you want to play only background music only for your application, then play it in the stream launched from your application / use the AsyncTask class to do this for you.
The concept of services is to run in the background; At its discretion, the value usually occurs when the user interface of the application is NOT visible . True, it can be used in the same way as yours (if you stop it), but it is simply not correct, and it consumes resources that you should not use.
If you want to perform tasks against a background of activity , use AsyncTask.
By the way, onStart deprecated. When you use services, run onStartCommand .
UPDATE:
I think this code will work for you. Add this class (included in your activity class).
public class BackgroundSound extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... params) { MediaPlayer player = MediaPlayer.create(YourActivity.this, R.raw.test_cbr); player.setLooping(true);
Now, to control the music, save the BackgroundSound object, rather than create it anonymously. Declare it as a field in your activity:
BackgroundSound mBackgroundSound = new BackgroundSound();
In your activity onResume method, run it:
public void onResume() { super.onResume(); mBackgroundSound.execute(null); }
And in your activity onPause method, stop it:
public void onPause() { super.onPause(); mBackgroundSound.cancel(true); }
This will work.