SopheakVirak
SopheakVirak

Reputation: 961

How to stop activity when user presses Home?

I am creating a music application. I'm having a problem: when the user presses back/home, the music continues playing.

Here is my APK file

How can I allow the user to return to Main Activity and clear the current activity (playing music)?

Upvotes: 0

Views: 424

Answers (5)

Manivannan
Manivannan

Reputation: 1640

Try to override the onWindowsFocusChanged(boolean hasFocus) method.

Upvotes: 1

Dharmaraj Patil
Dharmaraj Patil

Reputation: 116

When the user clicks back button the control goes inside the following method (Insert this method in your code)

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    super.onKeyDown(keyCode, event);
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                    // code to pause or stop the music!!!!
            }
            **return false;**
        }

Upvotes: 0

ashish.n
ashish.n

Reputation: 1224

toggle between the onpause and onstart is important The onPause() method is overrided to give playerback control to this instance. also u can refer this

package org.example.audio;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class AudioDemo extends Activity implements OnClickListener {
    private static final String TAG = "AudioDemo";
    private static final String isPlaying = "Media is Playing"; 
    private static final String notPlaying = "Media has stopped Playing"; 

    MediaPlayer player;
    Button playerButton;

    public void onClick(View v) {
        Log.d(TAG, "onClick: " + v);
        if (v.getId() == R.id.play) {
            playPause();
        }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        player = MediaPlayer.create(this, R.raw.robotrock);
        player.setLooping(false); // Set looping

        // Get the button from the view
        playerButton = (Button) this.findViewById(R.id.play);
        playerButton.setText(R.string.stop_label);
        playerButton.setOnClickListener(this);

        // Begin playing selected media
        demoPlay();

        // Release media instance to system
        player.release();

    }

    @Override
    public void onPause() {
        super.onPause();
        player.pause();
    }

    // Initiate media player pause
    private void demoPause(){
            player.pause();
            playerButton.setText(R.string.play_label);
            Toast.makeText(this, notPlaying, Toast.LENGTH_LONG).show();
            Log.d(TAG, notPlaying);
    }

    // Initiate playing the media player
    private void demoPlay(){
            player.start();
            playerButton.setText(R.string.stop_label);
            Toast.makeText(this, isPlaying, Toast.LENGTH_LONG).show();
            Log.d(TAG, isPlaying);
    }

    // Toggle between the play and pause
    private void playPause() {
        if(player.isPlaying()) {
          demoPause();
        } else {
          demoPlay();
        }   
    }
}

Upvotes: 0

Andro Selva
Andro Selva

Reputation: 54322

You can use your onPause() of your Activity to stop or pause the Mediaplayer.

Override the onPause() like this,

@Override
protected void  onPause()
{
    super.onPause();
  if(mediaplayer!=null)
{
    mediaplayer.pause();
}
}

And when you enter your activity once again, override the onResume() like this,

@Override
            protected void onResume() {
                super.onResume();

 if(mediaplayer!=null)
{
    mediaplayer.start();
}
}

Upvotes: 0

MByD
MByD

Reputation: 137272

You need to override onPause() or onStop() and stop the music there.

Upvotes: 1

Related Questions