开发者

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?


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


@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.


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜