How can I stop the MediaPlayer when Home key is pressed? - android

MediaPlayer player = MediaPlayer.create(context, R.raw.background);
player.start();
The Code above is part of the current code.
When I press the home key, MediaPlayer does not stop playing.
#Override
public boolean onKeyDown(int KeyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (KeyCode == KeyEvent.KEYCODE_BACK) {
System.exit(0);
return true;
}
}
return super.onKeyDown(KeyCode, event);
}
I've implemented the code shown above, for when you press the Back key.
Do you think that the above code is correct? Appreciate any help.

Just implement below methods for this,
#Override
protected void onPause() {
super.onPause();
if(player != null)
player.stop();
}
#Override
protected void onStop() {
super.onStop();
if(player != null)
player.stop();
}
#Override
protected void onDestroy() {
super.onDestroy();
if(player != null)
player.stop();
}

Please try with this solution:
public class PlayaudioActivity extends Activity {
private MediaPlayer mp;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
final TextView t = (TextView) findViewById(R.id.textView1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.far);
mp.start();
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.beet);
mp.start();
}
});
}
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}
For more information, please read this post: Android MediaPlayer Stop and Play

If your media player is running on the activity you created, you can call player.stop() in onStop() method.
Edit:
Your implementation is OK, but it only overrides the back key only. You have to override the home key also to stop the player on pressing the home key. (but, use onStop() instead as it calls when the user interface is hidden.)
Do not use System.exit(), use finish() instead.
If you using finish() method (which destroys the activity), make sure that you have released the MediaPlayer resources by calling player.release() and nullifying the player. (i.e. player = null)

Related

How to stop a MediaPlayer when I go to next activity in Android studio

public class momtahina5 extends AppCompatActivity {
private static final String TAG = "momtahina5";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_momtahina5);
Log.d(TAG,"onCreate: Starting.");
Button gotobakara201 = (Button) findViewById(R.id.gotobakara201);
Button backtobakara286 = (Button) findViewById(R.id.backtobakara286);
gotobakara201.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: clicked btngotobakara201");
Intent intent1 = new Intent(momtahina5.this,bakara201.class);
startActivity(intent1);
MediaPlayer mp = MediaPlayer.create(momtahina5.this,R.raw.mumfive);
mp.start();
}
});
}
}
What you can do is Release media player when activity is finished or destroyed.
below code will demonstrate you.
#Override
protected void onDestroy() {
super.onDestroy();
if (mp != null) {
mp.stop();
mp.reset();
mp.release();
}
}
override the 'onPause()' method of the current Activity and in that do this:
#Override
public void onPause() {
super.onPause();
if(mp != null) {
mp.release();
mp = null;
}
}
And also you want to make the MediaPlayer object as a class variable.
*Edit: It would be better to use the onPause() method instead of the onDestroy() method. When you move to another activity, the previous activity is not immediately destroyed. Refer to Android activity lifecycle for more info.

How to stop play sound by press button back or button home

How to stop play sound by press button back or button home
Please Help me :
public class MainActivity extends AppCompatActivity {
Button play;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button) findViewById(R.id.button_play);
final MediaPlayer mP = MediaPlayer.create(MainActivity.this,R.raw.music);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mP.isPlaying()) {
mP.pause();
play.setBackgroundResource(R.drawable.play);
} else {
mP.start();
play.setBackgroundResource(R.drawable.pause);
}
}
}
);
}}
Pause media player in onPause(); Method
#Override
protected void onPause() {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()){
mMediaPlayer.pause();
}
super.onPause();
}
Hope It will work for you.
For more information Refer MediaPlayer LifeCycle
to stop music player
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
to stop play sound by press button back or button home
in Activity you need to stop music on onDestroy and onBackPressed
#Override
protected void onDestroy() {
super.onDestroy();
stopPlaying();
}
#Override
public void onBackPressed() {
super.onBackPressed();
stopPlaying();
}

how to stop music if application in background

I'm trying to stop my music when the user leaves the app or clicks the home button. My application plays music even if it is in the background. How can I stop the music if the user clicks the home button? If the user returns, the music is played again.
Here is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.vaporv2);
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mediaPlayer.start();
}
});
//toggle off and on :
MusicButton = (ToggleButton) findViewById(R.id.toggleButton);
MusicButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (MusicButton.isChecked() && mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
mediaPlayer.start();
}
}
});
In your Activity:
Check for two states:
on pause state: When you press home button
#Override
protected void onPause() {
super.onPause();
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
}
on resume state: When you come back to your screen
#Override
protected void onResume() {
super.onResume();
if(mediaPlayer!=null && !mediaPlayer.isPlaying()){
mediaPlayer.start();
}
}
Since you are in Activity you can override onResume and onStop methods
and then play/pause your app.
#Override
protected void onResume() {
super.onResume();
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
}
#Override
protected void onStop() {
super.onStop();
mediaPlayer.pause();
}

How to avoid IllegalStateException with MediaPlayer

I have a stop button and a play button. When I click the play button, the audio plays fine and when I press stop, the audio stops. However, if I accidently hit the stop button twice in a row, an IllegalStateException gets thrown and force closes. How can I keep this force close from happening. Essentially what I want is that if someone accidently hits the stop button twice, the program does nothing. Here is the code:
private MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alphabet);
Button play = (Button) findViewById(R.id.button1);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp = MediaPlayer.create(getBaseContext(), R.raw.e_1100);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
});
Button stop = (Button) findViewById(R.id.button2);
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mp != null && mp.isPlaying()) {
mp.stop();
mp.release();
}
else if (mp.isPlaying() == false) {
// What do I put here???
}
}
});
}
}
try this
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}

How to stop audio from playing when back button is pressed?

Hi all here is my code:
final ImageView imageView = (ImageView) findViewById(R.id.Image7);
imageView.setImageResource(mFullSizeIds[position]);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(Audio.this,mAudio[position]);
mp.start();
}
});
Now this code works fine to play the audio when the user taps the image, however when I want to exit this activity and go to a different activity, the audio track is still playing, even when I press the home key the audio is still playing. How can I prevent this from happening?
Is there a way to stop the track from playing when the user taps the image again or presses the back/home button?
Add
#Override
public void onBackPressed ()
{
if (mp != null)
mp.stop();
super.onBackPressed();
}
and
#Override
public void onPause ()
{
if (mp != null)
{
mp.pause();
mp.stop();
}
super.onPause();
}
Create an onStop in the activity and make mp a class variable although I always create/prefer media player helper class for this sort of stuff. Then call mp.stop()/pause on onStop.
private MediaPlayer mp = null;
#Override
public void onStop() {
super.onStop();
if (mp != null) {
mp.stop();
}
}
then you just create it with removing the MediaPlayer before mp:
mp = MediaPlayer.create(Audio.this,mAudio[position]);
Override the onBackPressed method, and stop your MediaPlayer object, which is mp.

Categories

Resources