我尝试使用Android MediaPlayer服务来播放音乐。一切都很好,唯一的问题是MediaPlayer正在播放时按下按钮导致强制关闭并且下方at android.media.MediaPlayer.isPlaying(Native Method)
是我要播放的代码以及我如何尝试停止和关闭媒体播放器,任何想法?
播放:
private class Stream extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
mp.reset();
mp.setDataSource(url);
mp.prepare();
mp.start();
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
updateProgressBar();
} catch (Exception e) {
}
return null;
}
@Override
protected void onPostExecute(String result) {
mHandler.postDelayed(timeUpdate, 100);
}
}
闭合:
@Override
protected void onDestroy() {
super.onDestroy();
if (mp != null) {
mp.stop();
mp.release();
}
finish();
}
@Override
public void onBackPressed() {
super.onBackPressed();
onDestroy();
}
答案 0 :(得分:1)
替换
@Override
public void onBackPressed() {
super.onBackPressed();
onDestroy();
}
与
@Override
public void onBackPressed() {
if (mp != null) {
mp.stop();
mp.release();
}
finish();
super.onBackPressed();
}
试试吧......
答案 1 :(得分:0)
尝试实施onkeydown事件可能会有所帮助:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (mp != null) {
mp.stop();
mp.release();
}
finish();
return super.onKeyDown(keyCode, event);
}