Ami*_*avi 3 multithreading android android-mediaplayer
我在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);
}
};
Run Code Online (Sandbox Code Playgroud)
并且我在主活动中有一个选项菜单,点击该选项将退出活动
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();
}
Run Code Online (Sandbox Code Playgroud)
但它会抛出
android.media.MediaPlayer.getDuration(Native Method)中的java.lang.IllegalStateException
我知道线程中存在问题,应该在退出活动之前停止该线程,但我不知道究竟有什么人可以帮助PLZ?
使用以下
mhandler.removeCallbacks(mUpdateTimeTask);
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
802 次 |
| 最近记录: |