user3532857
user3532857

Reputation: 43

mediaplayer stop song when exit app or phone ring

My song is not stop when I hit home button or when I answer call? Also my timer is not stop when song is stopped? is there a way I can stop my timer? Could you please help me?

public class MainActivity extends Activity {

    private Button start, stop;
    private MediaPlayer mp;
    private TextView display, comment;

    private TextView timerValue;

    private long startTime = 0L;

    private Handler customHandler = new Handler();

    long timeInMilliseconds = 0L;
    long timeSwapBuff = 0L;
    long updatedTime = 0L;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        timerValue = (TextView) findViewById(R.id.timerValue);
        start = (Button) findViewById(R.id.bStart);
        start.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                mp = MediaPlayer.create(MainActivity.this, R.raw.splahsound);
                mp.start();
                startTime = SystemClock.uptimeMillis();
                customHandler.postDelayed(updateTimerThread, 0);

                mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                    @Override
                    public void onCompletion(MediaPlayer mp) {

                        mp.stop();
                        mp.reset();
                        mp.release(); // free up memory
                        mp = null;

                    }

                });

            }
        });

        stop = (Button) findViewById(R.id.bDur);
        stop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (mp != null) {
                    try {

                        mp.stop();
                        mp.reset();
                        mp.release();
                        mp = null;

                    } catch (Exception e) {
                        Log.d("error", e.toString());
                    }
                }
                timeSwapBuff += timeInMilliseconds;
                customHandler.removeCallbacks(updateTimerThread);
                            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();       
    }

    private Runnable updateTimerThread = new Runnable() {

        public void run() {

            timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

            updatedTime = timeSwapBuff + timeInMilliseconds;

            int secs = (int) (updatedTime / 1000);
            int mins = secs / 60;
            secs = secs % 60;
            // int milliseconds = (int) (updatedTime % 1000);
            timerValue.setText("" + mins + ":" + String.format("%02d", secs));
            customHandler.postDelayed(this, 0);
        }

    };

}

Upvotes: 2

Views: 1545

Answers (2)

ogranada
ogranada

Reputation: 131

you need override the onPause and onResume to stop when you push the Home button

@Override
public void onPause() {
    super.onPause();  // Always call the superclass method first
    mp.pause();
    // stop the clock
}

@Override
public void onResume() {
    super.onPause();  // Always call the superclass method first
    mp.start();
    // re-sync the clock with player...
}

If you need stop any thing when your phone rings and your app run in background you need subscribe your app to PHONE_STATE, see this link

Upvotes: 1

donfuxx
donfuxx

Reputation: 11323

You can stop MediaPlayer in the onPause method:

@Override
protected void onPause() {
    //stop mediaplayer:
    if (mp != null && mp.isPlaying()) {
        mp.stop();
    }
    super.onPause();       
}

Instead of:

@Override
protected void onPause() {
    super.onPause();       
}

Note: the check if MediaPlayer is currently playing is important, because calling stop() method on a MediaPlayer that is not currently playing will throw an IlegalStateException.

Upvotes: 3

Related Questions