0

I want to play music in the background of my application:

public class BackgroundSoundService extends Service {

    MediaPlayer mediaPlayer;

    @Override
    public void onCreate() {
        super.onCreate();
        mediaPlayer = MediaPlayer.create(this, R.raw.airport_lounge);
        mediaPlayer.setLooping(true);
        mediaPlayer.setVolume(100,100);
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mediaPlayer.start();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mediaPlayer.stop();
        mediaPlayer.release();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

Then in my MainActivity :

public static Intent musicIntent;

//Start music service
        musicIntent = new Intent(this, BackgroundSoundService.class);
        startService(musicIntent);

 @Override
    protected void onPause() {
        super.onPause();
        stopService(musicIntent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        startService(musicIntent);
    }

I stop it in my onPause the service because if the user switches the Application and doesn't close it the music would continue to play. The problem is that when we change Activity the onPaused is also called but I want the music to continue playing through the entire application.

So in my another Activity :

//start music
    startService(MainActivity.musicIntent);

But the music restarts and it's not very good to hear. Any solutions?

AliSh
  • 10,085
  • 5
  • 44
  • 76
Loic Jackotin
  • 245
  • 2
  • 10

2 Answers2

3

You can use ActivityLifecycleCallbacks in your application class.

public class MusicApplication extends Application implements ActivityLifecycleCallbacks {
    int activitiesOnTop = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(this);
    }


    @Override
    public void onActivityResumed(Activity activity) {
        // one of application activities is visible
        activitiesOnTop ++;
    }

    @Override
    public void onActivityStopped(Activity activity) {
        activitiesOnTop --;
        if(activitiesOnTop == 0){
              // none of activities are on foreground
        }
    }
}

There are other solutions mentioned in the How to check if activity is in foreground or in visible background?.

AliSh
  • 10,085
  • 5
  • 44
  • 76
  • Unfortunately it doesn't help me because these methods are called each time that an activity onStop, on resumed is called etc... What I want is when we change activity the music is not affected but when we put the application in background so the music stop because the music must not play when the user is in another application. – Loic Jackotin Jan 06 '21 at 20:56
  • @LoicJackotin I have updated my answer, please check it – AliSh Jan 06 '21 at 21:21
0

It depends on the application structure, for example, if you build some tab or navigationDrawer based navigation, you will likely build some fragments and your MainActivity will be your only activity, so it's onPause method will only be called if user closes your application. But this scenario won't hold as your application grows (unless it's quite simple). So another approach is to launch your service in your application instance, in case you use it.

javier Cuervas
  • 823
  • 10
  • 11