0

I have trouble to play music across all activity, I had implement service and handle onPause to stop the music when going to background (not visible to user).

The problem is when i navigate to another activity, the onPause method is called that make my music stop. How to fix this issue? I need to play my music across all my activity in foreground only and dont wan to play it when the application in the background. Appeciate anyone know how to solve this.

This is my base activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    startIntent = new Intent(getApplicationContext(), MusicService.class);
    if(binder==null){
        bindService(startIntent,this, Context.BIND_AUTO_CREATE);
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if(binder.getMyService()!=null){
        stopService(startIntent);
        unbindService(this);
    }


}


@Override
protected void onStart() {
    super.onStart();

}


@Override
protected void onPause() {
    super.onPause();

    if(binder!=null) {
        binder.getMyService().pauseMusic();
    }


}



@Override
protected void onResume() {
    super.onResume();
    if(binder!=null){
        binder.getMyService().resumeMusic();
    }
}




@Override
public void onServiceConnected(ComponentName name, IBinder service) {
    binder = (MusicService.Binder) service;

}

@Override
public void onServiceDisconnected(ComponentName name) {

}

this is my mainactivity extends base activity

@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    startIntent = new Intent(MainActivity.this, MusicService.class);
    startService(startIntent);


}


@Override
protected void onDestroy() {
    super.onDestroy();

}

public void how(View view) {
        Intent intent = new Intent(this, AboutActivity.class);
        this.startActivity(intent);



    }
Ng Yong Loon
  • 27
  • 2
  • 13

3 Answers3

0

Don't stop the audio play in onPause() as Harin Kaklotar suggested. You can use his method of onDestroy() or you can have it in an asynchronous task and turn off the sound by using surfaceDestroyed(). You can refer the android documentation of AudioManager if you need anything else of the sort. https://developer.android.com/reference/android/media/AudioManager.html

Hope I Helped :D

UPDATE

You can create a system to check if your app is in the foreground or background. This involves counting how many activities are paused. Add this to all your activities:

@Override
protected void onPause() {
    super.onPause();
    MainActivity.Pause++;
}

@Override
protected void onResume() {
    super.onResume();
    MainActivity.Pause--;
}

And in your MainActivty,

  if (Pause == NUMBER_OF_ACTIVITIES) {
      //PAUSE MUSIC HERE
  }
Ishan Manchanda
  • 459
  • 4
  • 19
  • ondestroy only perform when my app is destroy.... if i press home button or menu button to navigate another app its still playing the music – Ng Yong Loon Oct 17 '16 at 13:31
0

You could implement code that will track current activity in your app. (Good example: How to get current activity)

And stop music only when "current activity" is null.

PS: depending on your implementation of tracking current activity you might want to check current activity not onPause() right away but with some delay .

Community
  • 1
  • 1
Gugalo
  • 42
  • 4
  • I might have misunderstood you, but I think that the problem is that in your app onResume() of new activity happens before onPause of privies activity. And that stops your music. And this solution could fix this. Please try to log events of pause and resume of activities to confirm if that is the issue. – Gugalo Oct 17 '16 at 13:44
  • @Override protected void onPause() { super.onPause(); Activity currentActivity = ((BaseApplication)getApplicationContext()).getCurrentActivity(); if(binder!=null&&currentActivity==null) { binder.getMyService().pauseMusic(); } } – Ng Yong Loon Oct 17 '16 at 13:59
  • I add this in my onPause, its get null and music stop in another activity – Ng Yong Loon Oct 17 '16 at 14:00
  • 2
    I use @DaveThomas method to detect foreground.... i put it in onStop method... its work... thanks – Ng Yong Loon Oct 17 '16 at 14:11
  • Ok, I have only one last idea (I might be on completly wrong track). Could you try: ` @Override protected void onPause() { super.onPause(); new Handler().postDelayed(new Runnable() { @Override public void run() { Activity currentActivity = ((BaseApplication) getApplicationContext()).getCurrentActivit‌​y(); if (binder != null && currentActivity == null) { binder.getMyService().pauseMusic(); } } }); } ` – Gugalo Oct 17 '16 at 14:16
0

You have to use ActivityLifecycleCallbacks to tell if you app is in the foreground or background.

For example:

public class DummyActivityLifecycleCallbacks implements ActivityLifecycleCallbacks {
    @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { }
    @Override public void onActivityStarted(Activity activity) { }
    @Override public void onActivityResumed(Activity activity) { }
    @Override public void onActivityPaused(Activity activity) { }
    @Override public void onActivityStopped(Activity activity) { }
    @Override public void onActivityDestroyed(Activity activity) { }
    @Override public void onActivitySaveInstanceState(Activity activity, Bundle savedInstanceState) { }
}

public class OnApplicationForegroundBackgroundEnterCallbacks extends DummyActivityLifecycleCallbacks {
    private Listener listener;
    private int activityStartStopCounter = 0;


    public interface Listener {
        public void onEnterForeground();
        public void onEnterBackground();
    }


    public OnApplicationForegroundBackgroundEnterCallbacks(Listener listener) {
        this.listener = listener;
    }

    @Override public void onActivityStarted(Activity activity) {
        if(++activityStartStopCounter == 1) listener.onEnterForeground();
    }

    @Override public void onActivityStopped(Activity activity) {
        if(--activityStartStopCounter == 0) listener.onEnterBackground();
    }
}

Then in Application.onCreate call:

registerActivityLifecycleCallbacks(new OnApplicationForegroundBackgroundEnterCallbacks(this));