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();
}
}
Related
A part of my code: (Problem Explanation follows)
#Override
protected void onStop() {
super.onStop();
if(mediaPlayer != null){
mediaPlayer.release();
mediaPlayer = null;
}
}
My main idea of using this was to stop playing the audio when the home button was pressed, but this doesn't seem to be happening as audio isn't stopping.
Thanks in advance.. :)
You are hooking into the Activities onStop() method to stop the audio however when you press the home button, an Activities state is being saved, and therefore the activity calls its onPause() method instead of onStop().
If you want to stop audio when the home button is pressed, I suggest moving your call to the media player that's stopping it, into the Activities onPause() method.
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.
i have a mediaplayer that streams music in a service
in my main activity i click on a toggle button the music begins to stream and the image for the toggle button change from play to stop when i leave my app the music stays playing but when i return to the app the togglebutton is back to play i tried if statements but it doesnt work here is a example what i tried
i tried this code in my service
if (mediaPlayer.isPlaying()){
MainActivity.playStop.setBackgroundResource(R.drawable.stopbtn);
}
in your Playbackservice create static variable isPlaying. Then in your activity onResume() use something like
onResume(){
if(Playbackservice.isPlaying){
set toggle on
}else{
set toggle off
}
}
I am building media player application in that on First Activity I am Showing All Song List and on song click i am playing song in media player. But when i back pressed i come to the song list again and again when i back pressed i come out of the application. I want that song should be continue to play when i come out of the application and when i resume to my application it continues to play.
Plz Suggest me some solution.
I have tried this code :
#Override
public void onBackPressed() {
// do whatever you desire
moveTaskToBack(true);
return;
}
But this is not working. When i resume to my application it throws error.
in the songsplay list activity, after the startactivity(intent) write finish();
it will finish() the activity, now we can press back button songsplay list activity will not appear.
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();
}