Splitusa
Splitusa

Reputation: 1181

Media Player streaming, when Exit app music should stop

I currently have a mediaplayer streaming some mp3 files. What I would like to do is if the User presses the home button to exit the app, the music stops playing. how would I go about doing this?

Upvotes: 0

Views: 5030

Answers (3)

Sruit A.Suk
Sruit A.Suk

Reputation: 7273

Add this method to your class to override your home button

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME) {
            media.stop();
            finish();
            //or complete end your application
        //System.runFinalizersOnExit(true);
        //System.exit(0);

        return true;
    }
    return false;
}

Upvotes: -1

Neeraj Nama
Neeraj Nama

Reputation: 1572

In your app' onPause() or onDestroy() call mediaPlayer.stop().

Upvotes: 10

yep
yep

Reputation: 94

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) {     
if ((keyCode == KeyEvent.KEYCODE_HOME)) {         
Log.d(this.getClass().getName(), "home button pressed");     }     
return super.onKeyDown(keyCode, event); } 

Something like this, except instead of log, do whatever you do in your implementation to kill the service/asynctask/thread handling your music.

Upvotes: -1

Related Questions