I followed this answer which uses service class to create play background music for all activities in the app.
The problem I have is that when a user presses back button to go to previous activity, or clicks on home button, or recent apps button and relaunches the app, the music starts all over again. I want the music to resume where it left off.
The music should start over ONLY when the user exits the app via the exit button that I have in my app.
So here's what I have done in my onPause and onResume methods:
@Override
protected void onPause(){
super.onPause();
if (!this.isFinishing()){
Intent svc=new Intent(getApplicationContext(), BackgroundSoundService.class);
stopService(svc);
}
}
@Override
protected void onResume(){
super.onResume();
Intent svc=new Intent(this, BackgroundSoundService.class);
startService(svc);
}
These two methods are there in every activity.
Also, to start the music, I used the following code in the MainActivity (Launcher Activity) of my class:
Intent svc=new Intent(this, BackgroundSoundService.class);
startService(svc);
If you look at the onPause() method, you will see that I used stopService() method to stop the music. I could not find pauseService() or something similar to pause the music. Can anyone help me on this one?