Android MediaPlayer, SeekBar Exception when change activity

I am currently working on an android application with audio playing function. In the play_pause Activity, I have a seekbar and a button to control the audio.

public class PlayerActivity extend AppCompatActivity {
    ...
    ...
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        playerButton = (Button) findViewById(R.id.playButton);

        playerButton.setOnClickListener(new Button.setOnClickListener() {
                if (player != null) {
                     //load music to player
                     player.create(PlayerActivity.this, R.raw.music);
                     seekbar.setMax(player.getDuration());

                     mTimer = new Timer();
                     mTimerTask = new TimerTask() {
                         public void run() {
                             seekbar.setProgress(player.getCurrentPosition());;
                         }
                     };
                     mTimer.schedule(mTimerTask, 0, 10);
                }
        });
    }
}

When I press the back button without hit the "play" button, my application can change activity successfully. However, once the "play" button has been clicked, I can not change activity. Below is the onDestroy() method.

protected void onDestroy() {
    super.onDestroy();
    player.release();
}

The Logcat has the error message saying

java.lang.IllegalStateException
at MediaPlayer.getCurrentPosition()
at PlayerActivity.run() //which is the mTimerTask
at java.util.Timer$TimerImpl.run()

Anyone have idea how to solve?

Answers


Cancel your Timer onDestroy of Activity too by calling

mTimer.cancle();

Exception raised due to call of player.getCurrentPosition(); after release of Media player instance on destroy of Activity as you timer thread dont stops.


Need Your Help

xlwt: write multiple columns

python excel xlwt

I have a file containing data like below

Bundler using an older Ruby version

ruby-on-rails ruby bundler

I have Ruby 2.3.0 installed in my system. The output of ruby -v is: