您的位置:首页 > 移动开发 > Android开发

android使用mediaPlayer显示视频时,抛出java.lang.IllegalStateException异常

2015-12-24 10:58 656 查看
使用Mediaplayer播放视频时,自定义了进度条显示进度,一次需要使用run方法来实时监测视频播放的位置,代码如下:Runnable runnable = new Runnable() {

@Override
public void run() {
if (player != null) {
int pos = player.getCurrentPosition();
if (pos > 0) {
int duration = player.getDuration();
int ss = (int) (player.getCurrentPosition() * 100)
/ duration;
progress.setProgress(ss);
video_time.setText(TimerUtils.formatTimes(player
.getCurrentPosition()));
if (pos == duration) {
player.pause();
btn_player
.setBackgroundResource(R.drawable.bt_view_play);
}
} else {
video_time.setText("00:00");
progress.setProgress(0);
}
}
view_handler.postDelayed(runnable, 1000);
}
};

但是在退出该activity时,总是会报错java.lang.IllegalStateException,而位置则是指向 player.getCurrentPosition(),

在onDestroy方法中relese掉了mediaplayer,但是仍会报错;后来发现,当退出该activity时,将其destroy掉了,但是只是释放掉player是不够的,同时还要增加判断

                if (player != null) {
player = null;
}

将mediaplayer设置为空,runnable 中的内容就不会在退出activity时继续执行,而引起异常了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息