Dealing with Android's MediaPlayer while rotating screen, pressing Home Button or App is interrupted -


I am in a difficult part of the workflow / design on my app.

I have a media player that runs in a service, I want to stop the music when the user leaves (but not closed) the app is the same, the home button

MediaPlayer's breaks () and start () are implemented in the activity of onStop () and onResume () , Respectively, this button is working properly and relabeling the app, but it happens even when the activity cycle is due to Screen rotates.

So, how can I save it efficient / elegant way? Music should not be interrupted when the device is rotated. I thought of overriding the click method of the Home button, stop there mediaplayer and set a global boolean flag to check in onResume () if the media player must start again. But it seems like I do not have a hack or such a good practice.

Am I wrong with this?

Is there a better way?

If your minSdkVersion is at least 14, you can see the isChangingConfigurations () flag before pausing the media player:

 Protected Zero at Overwrite (@) {if (Changing Configuration is) {// Stop Media Player}}   

Otherwise, you can see it on UserLeaveHint () :

  Override Protected Zero User Lev Hunt () {// Stop Media Player}   

However any other application Call forcibly Will not get drawn on the foreground (like a phone call arrives).

Edit: As an alternative you may be able to rely on onWindowFocusChanged () . It is called with false when skipping activity with home or back, but not on configuration changes. Public overover override {// media player}

It should also be called by

  @WindowFocusChanged (Boolean hashfocus). As a side note, you should generally use symmetric lifecycle methods (for example  onPause () ,  onStart ()  use  onPause ()  /  onResume ()  or  onStop ()  /  onStart () )   

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -