Media Player streaming, when Exit app music should stop

狂风中的少年 提交于 2019-12-12 01:53:53

问题


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?


回答1:


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




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/6457929/media-player-streaming-when-exit-app-music-should-stop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!