我在android中创建了一个mediplayer,它有一个可运行的后台线程,用于保持时间导引头的更新
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
long totalDuration = mediaPlayer.getDuration();
long currentDuration = mediaPlayer.getCurrentPosition();
// .... setting textviews of durations
// ... converting milli to mm:ss and setProgress(progress)
mHandler.postDelayed(this, 100);
}
};
并且我在主要活动中有一个选项菜单,点击该选项将退出活动
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.exitplayer)
exitPlayer();
return super.onOptionsItemSelected(item);
}
private void exitPlayer() {
mediaPlayer.stop();
mediaPlayer.release();
try {
mUpdateTimeTask.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
this.finish();
}
但它会抛出
java.lang.IllegalStateException at android.media.MediaPlayer.getDuration(Native Method)
我知道线程中存在问题,应该在退出活动之前停止该线程,但我不确切知道如何 任何人都可以帮助PLZ?
答案 0 :(得分:5)
使用以下
mhandler.removeCallbacks(mUpdateTimeTask);
http://developer.android.com/reference/android/os/Handler.html
public final void removeCallbacks (Runnable r)
Added in API level 1
Remove any pending posts of Runnable r that are in the message queue.