MediaPlayer and threading cleanup before onBackPressed

I have a mediaplayer in my activity and with the mediaplayer I have a seekbar that I am updating via a thread handler. The way I run the thread handler is like so:

Runnable run = new Runnable() {

        @Override
        public void run() {
            seekUpdation();
        }
    };

    public void seekUpdation() {
        if(mp.isPlaying()){
            this.seekbar.setProgress(mp.getCurrentPosition());
            this.seekHandler.postDelayed(run, 2);
        }
    }

I call this whenever the user hits the play button and then the seekbar keeps getting updated while the mediaplayer is in playing. SO this works great, but when I hit the Back button when the mediaplayer is playing I then get an illegalstateexception because I am doing the following: @Override public void onBackPressed(){

    super.onBackPressed();
    if (mr != null)
        mr.release();
    if (mp != null){
        mp.pause();
        mp.stop();
        mp.release();
    }
}

See I figured by pausing the mediaplayer this would trigger the thread to stop, but it hasn't worked. Does anyone have any other suggestions?

链接地址: http://www.djcxy.com/p/88598.html

上一篇: 垂直Seekbar setProgress不工作

下一篇: MediaPlayer和onBackPressed之前的线程清理