Pass a Mediaplayer in an other Activity - android

I have a Mediaplayer mpFond, started in my first activity. If I press the HOME, the mediaplayer is stopped, and if i press "PLAY" to go in my other activity, the sound continu (that's good). When I press the Home button in my first activity (where I started the mediaplayer), it's ok, my sound stop, but if I press the Home button in the other activity, it does not (and that's my problem).
So I just want to know if I can pass the Mediaplayer in the other activity to stop it when I press the Home button (without using Services, i'm new, so ...)
I just use that to stop the sound when I press the Home button :
#Override
public void onPause()
{
super.onPause();
if(this.isFinishing()){
mpFond.stop();
}
}
#Override
public void onStop(){
super.onStop();
if(this.isFinishing()){
mpFond.stop();
}
}

For some reasons, there is no way you can pass the MediaPlayer object to another activity through intent. Maybe it is because activity only passes Parcelable or Serializable objects. But I just discovered another way. To pass MediaPlayer to another activity you need to
1- create the MediaPlayer inside your application object.
2- Access the MediaPlayer from whatever activity you want and bind it over again to it.

Related

How to stop music when clicking to new activity and be able to start it again from the beginning?

I have an audio playing in the background when I am clicking onto a new activity. I want while I am clicking to the next activity, for the audio, to stop.
However, when going back to the original activity I want the audio to start again from the beginning.
Using media player and Android Studio. Here's all the media player code I have so far:
MediaPlayer mediaPlayer = MediaPlayer.create(pagetwo.this,R.raw.audio1);
mediaPlayer.start();
You can use the basic lifecycle methods of your Activity to handle this use case:
Android will call onPause() if you open a new Activity and will call onResume() if you enter/come back to the first activity.
If you call mediaPlayer.start() in onResume() and mediaPlayer.stop() in onPause() you should have the desired behavior.

In Android, how to resume music after returning from pressing back/home button

In Android, When I press back/home button the music stops (that's fine), but when I re-enter to the activity I want to begin/resume playing back the music.
How does Android manage playback of audio when the program goes into the background?
The code given below only works if the activity hasn't been destroyed. If you want to survive when the user presses the back button, you'll have to write the current status of the media player out to a file, and reload it when the activity starts again.
I'm not sure that's the correct behavior though. After the back button is pressed, your application SHOULD terminate, and start up next time in a reset state. I think. If the back button gets used for navigation in your app, the solution is to put up an alert asking of the user REALLY REALLY wants to quit, on the final back button that terminates the app. Override
Activity.onBackPressed to customize the behavior of the back button.
Note also that your activity can be destroyed even in paused state, if the system is looking for more memory, if the screen flips, or if several other high-level system states change. To deal with this case, you need to implement savedInstanceState handling. Implement Activity.onSaveInstanceState, and then look for a non-null savedInstanceState parameter in your implementation of onCreate.
In both cases, you're going to have to implement some method for recreating the media player, preparing the audio again, and seeking to the correct position in the resurrected audio.
Ordinarily, you should include a lot more details and code about your problem, but I just happened to see your other post on a related matter.
You need the following in onResume(). Note, if you are doing mp.start() in onCreate(), you can remove that start as onResume() is called after onCreate().
#Override
protected void onResume() {
if(mp != null && !mp.isPlaying())
mp.start();
super.onResume();
}
start() from MeidaPlayer will start over if stop or never played, or from where paused if it was paused.
add this method,
#Override
protected void onResume() {
super.onResume();
if(mp != null)
mp.start();
}

MediaPlayer Stop

How stop playing audio when click back button ? or exit application it's keeping playing on background
I made application has 2 activities first Main welcome activity you click on start button show second activity which contain grid view for multiple images when click on an image start play different sound , but when click back button switch to Main activity but sound keep playing !! and if click home also keep playing?
how to stop it ?
Tell it to stop in the onPause() of the Activity.
#Override
public void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}

How do I get music to resume playing?

I have music start playing in Activity A. When Activity B opens, Activity A closes, as well as the music from Activity A stops playing. When I return to Activity A, I would like the music to pick up playing from where it left off. How do I do this?
you need to store (persistently) the position that your media was at in onPause() or onStop() of activity A, and then in onResume() check the value that was stored and call seekTo() on your mediaplayer. Something like this:
to get your SharedPreferences objects:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor prefsEdit = prefs.edit();
To store and recall the position.
public void onPause(){
super.onStop();
//It might make more sense to do this right before you call startActivity()
//to launch activityB then here.
int position = mp.getCurrentPosition();
prefsEdit.putInt("mediaPosition", position);
prefsEdit.commit();
}
public void onResume(){
super.onResume();
int position = prefs.getInt("mediaPosition", 0);
mp.seekTo(position);
}
Note if you use this method you'll also need to set the position to 0 when you are exiting ActivityA without going to B. Otherwise when you come back to it the music is not going to start at the beginning.
In Activity A, override onPause() method to pause the MediaPlayer and override onResume() to resume the MediaPlayer. And make sure that the mediacontroller object doesnt get destroyed when going to Activity B.
See actually real time media players are handled on services but not on the activities if you observe it carefully.. So my suggestion is that try writing media player in service and better use life cycle methods of the media player class and the activity with service

Music in Webview Activity kill when press back button to home activity

I am coding the play mp3(Flash Embedded) in WebView(Second Activity), I can play the music in WebView as usually, but when I press back buttom to First Activity the music in Second Activity in WebView still playing, then I go to Second Activity again in WebView to stop the music but it's appear the new mp3 play list screen, I can't stop the music.
So, how can I kill the process when I press back button to First Activity(Home Screen)
Best Regards,
Virak
write this function in your class. actually this will override onpause.
#Override
protected void onPause() {
super.onPause();
finish();
}
This was the only thing that worked for me. To use the destroy() method of WebView in the onPause() lifecycle method.
#Override
public void onPause(){
super.onPause();
wView.destroy();
}

Categories

Resources