Playing MediaPlayer sound between actions, but pause / resume when the background loses focus and returns?

I spent almost the whole day trying to find a solution for a specific MediaPlayer problem. I would like my MediaPlayer sound to continue playing when the user switches the actions in my application, but stops when switching to another application (the "Home" button, the "Recent Applications" button, etc.) And it resumes when the application gets again out of focus. I just can't find a solution that works that way, though. It’s not that I don’t know where to start, but every solution I tried was complicated, and in the end it just didn’t work. It seems like I just need the application to be able to say when Activity stops switching actions and lose focus from the application.

I cannot imagine that such a situation is so unusual that there is no neat and elegant solution to solve it. Can someone help me figure it out? Thanks!

+4
source share
3 answers

You will need to implement the Service and BroadcastReceiver , which listens for the required actions and / or keystrokes (the "Home" button, etc.) using the intent filter. Android media player source can be downloaded from here . The two classes you want to look at are MediaButtonIntentReceiver and MediaPlaybackService .

+3
source

I think the best way to share MediaPleer between actions is the android Service. Create a service to store MediaPlayer. Your actions are bound to the service in onResume and untied to onPause. The service always knows if a person is attached to it. Therefore, if no one is attached (there is no your action), he stops playing. google "android MediaPlayer from service"

+4
source

It may happen that the service. With the service or not, although it can often be a sign that the program you developed is not as reliable as it should be. Even if you create a service, you still need to know when to pause and when to resume it.

I implemented a simple solution that seems to work for me without rebuilding the whole application in order to have a better mechanism for working with applications:

After launching mediaPlayer in the initial activity in which you want to start music:

In the initial and each subsequent action, create a member variable: private boolean mStopTheMusic = true;

Then, in EVERY active action that you want the music to continue to play and stop, it redefines the following accordingly: (note: the stop will be properly when any other application appears in the foreground, not yours. These are suitable moments to stop music) And at any time when you programmatically start a new activity on top of the current one, set mStopTheMusic = false so that your background music does not stop.

@Override public void onResume() { super.onResume(); mStopTheMusic = true; if (!MusicPlayer.IsMusicPlaying()) MusicPlayer.StartMusic(); } @Override public void onPause() { super.onPause(); if (mStopTheMusic) MusicPlayer.PauseMusic(); } @Override public void onBackPressed() { mStopTheMusic = false; super.onBackPressed(); } 
0
source

All Articles