0

I'm working on a game with a MainActivity which opens activities with the game levels. Each of the level activities inherits a BaseActivity.

MainActivity never finishes. All the levels open on top of it.

I want to start my music in the MainActivity and keep it playing throughout the rest of the application.

The catch is that I want the Service to pause the music when the app goes to the background.

I've created a MusicService which plays the music through the MediaPlayer like so -

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

I'm confused about where to pause the music. The Service is started from the MainActivity but I want to be able to also pause it even when the app is paused while on any Level activity. Can I communicate with the Service through any Activity?

I looked at this question but I don't want my music to stop and then restart every time I destroy and create a Level activity.

Mallika Khullar
  • 1,725
  • 3
  • 22
  • 37
  • You can create notification For example [http://www.tutorialsface.com/2015/08/android-custom-notification-tutorial/](http://www.tutorialsface.com/2015/08/android-custom-notification-tutorial/) – Gautam Surani Aug 08 '18 at 09:29

2 Answers2

1

Simply use EventBus

Gradle dependancy -

implementation 'org.greenrobot:eventbus:3.1.1'

In your main activity onCreate

EventBus.getDefault().register(this);

In your main activity onDestroy

EventBus.getDefault().unregister(this);

In your main activity write method like below. This method will always call when you post your event from any activity

@Subscribe(threadMode = ThreadMode.MAIN)
public void onPauseEvent(event: PauseEvent) {

}

And last step post your event from any activity or non activity classes

EventBus.getDefault().post(new PauseEvent("delete"))
Chetan Shelake
  • 656
  • 1
  • 6
  • 14
0

Use a BroadcastReceiver and register it, in your service

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
        registerReceiver(new Player(), new IntentFilter(App.Communication.Player));

    return START_STICKY;
}

private class Player extends BroadcastReceiver {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onReceive(Context context, Intent intent) {

            Bundle extras = intent.getExtras();

            int action = extras.getInt(App.Communication.IntentAction);
            if(action == App.Communication.ActionStart){

                isPlaying = true;

                String url = extras.getString(App.Communication.URL);
             //Start player from url
            }
            else if(action == App.Communication.ActionPlay ){
                urlPlayer.start();
            }
            else if(action == App.Communication.ActionPause ){
                //Pause Player
            }
            else if(action == App.Communication.ActionStop ){
                //Stop player
            }
            else if(action == App.Communication.ActionSeekForward ){
                //Seek Forward
            }
            else if(action == App.Communication.ActionSeekBack ){
                //Seek Back}

        }
    }

Create a simple App class to hold some static values

public class App  {

    public class Communication{
        public static final String Player = "Player";
        public static final int ActionStart = 0;
        public static final int ActionPlay = 1;
        public static final int ActionPause = 2;
        public static final int ActionStop = 3;
        public static final int ActionSeekForward = 4;
        public static final int ActionSeekBack = 5;
        public static final int ActionPlayRadio = 6;


    }
}

And lastly in any Activity simple call the BroadCast. For example to play

    Intent intent = new Intent(App.Communication.Player);
    Bundle mBundle = new Bundle();
    mBundle.putInt(App.Communication.IntentAction,App.Communication.ActionPause);

    intent.putExtras(mBundle);
    mContext.sendBroadcast(intent);
Niza Siwale
  • 2,390
  • 1
  • 18
  • 20