Java.lang.illegalStateException randomly throws when receiving mediaPlayer.getCurrentPosition

This code works fine with a song, but for some time, accidentally moving to the next song manually will cause the application to crash. Happens by chance

    updateSeekBar = new Thread() {
        @Override
        public void run() {
            int runtime = mediaPlayer.getDuration();
            int currentPosition = 0;
            while (currentPosition < runtime) {
                try {
                    sleep(500);
                    currentPosition = mediaPlayer.getCurrentPosition();//This is where the app crash                        
                    if (seekBar != null) {
                        seekBar.setProgress(currentPosition);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    };

Crash report

06-10 22:08:53.160 15351-15560/skydeveloper.me.musicx2 E/AndroidRuntime: FATAL EXCEPTION: Thread-6875
                                                                     Process: skydeveloper.me.musicx2, PID: 15351
                                                                     java.lang.IllegalStateException
                                                                         at android.media.MediaPlayer.getCurrentPosition(Native Method)
                                                                         at skydeveloper.me.musicx2.Player$1.run(Player.java:104)
+4
source share
6 answers

In fact, this problem occurs if Media Player is in the wrong state.

If your MediaPlayer is in Initialized-State, you cannot call start(). So you need to wait while your MediaPlayer is in position Prepared-State. You can search for a media planner only in a state of readiness, start and pause.

:

. , .

mediaPlayer.reset();

try {
   .......
   .......
   currentPosition = mediaPlayer.getCurrentPosition();
   ......
} catch (IllegalStateException e) {
   mediaPlayer.reset();
   currentPosition = mediaPlayer.getCurrentPosition();
}

mediaPlayer.prepareAsync();

: java.lang.IllegalStateException MediaPlayer.getCurrentPosition



Update1:

, . , .

try {
....
currentPosition = mediaPlayer.getCurrentPosition();
....
} catch (final Exception e) {
    e.printStackTrace();
    if (e instanceof IllegalStateException) { // bypass IllegalStateException
        .......
        // You can again call the method and make a counter for deadlock situation or implement your own code according to your situation
        if (retry) {
               mediaPlayer.reset();
               currentPosition = mediaPlayer.getCurrentPosition();
        } else {
            throw e;
        }
    }
}

UPDATE2:

, . , .

 try {
     ....
     currentPosition = mediaPlayer.getCurrentPosition();
     ....
     } catch (final Exception e) {
         e.printStackTrace();
         if (e instanceof IllegalStateException) { // bypass IllegalStateException
             .......
             // You can again call the method and make a counter for deadlock situation or implement your own code according to your situation
             boolean checkAgain = true;
             int counter = 0;
                 for(int i = 0; i < 2; i++){
                     if (checkAgain) {
                         mediaPlayer.reset();
                         currentPosition = mediaPlayer.getCurrentPosition();
                         if(currentPosition > 0) {
                             checkAgain = false;
                             counter++;
                         }
                     } else {
                         if(counter == 0){
                             throw e;
                         }
                     }
                 }


         }
     }

UPDATE4:

  • IllegalStateException, prepare() prepareAsync() .
  • "" , /, screenOnWhilePlaying, , .

  • , start(). start() , MediaPlayer Started. isPlaying() , , MediaPlayer .

  • "" OnBufferingUpdateListener.onBufferingUpdate() , OnBufferingUpdateListener setOnBufferingUpdateListener(OnBufferingUpdateListener). /. start() MediaPlayer, Started.

, isPlaying() . , :

 try {
     ....
     currentPosition = mediaPlayer.getCurrentPosition();
     ....
     } catch (final Exception e) {
         e.printStackTrace();
         if (e instanceof IllegalStateException) { // bypass IllegalStateException
             .......
             // You can again call the method and make a counter for deadlock situation or implement your own code according to your situation
             boolean checkAgain = true;
             int counter = 0;
                 for(int i = 0; i < 2; i++){
                     if (checkAgain) {
                         mediaPlayer.reset();
                         if(mediaPlayer != null & mediaPlayer.isPLaying()) {
                            currentPosition = mediaPlayer.getCurrentPosition();
                         } else {
                            currentPosition = 0; 
                         }
                         if(currentPosition > 0) {
                             checkAgain = false;
                             counter++;
                         }
                     } else {
                         if(counter == 0){
                             throw e;
                         }
                     }
                 }


         }
     }
+3

, , 500 , ! , . , . , :

updateSeekBar = new Thread() {
        @Override
        public void run() {
            int runtime = mediaPlayer.getDuration();
            int currentPosition = 0;
            int adv = 0;
            while ((adv = ((adv = runtime - currentPosition) < 500)?adv:500) > 2) {
                try {
                    currentPosition = mediaPlayer.getCurrentPosition();
                    if (seekBar != null) {
                        seekBar.setProgress(currentPosition);
                    }
                    sleep(adv);    
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    seekBar.setProgress(runtime);
                    break;
                }

            }
        }
    };

, 2 (2 - , , 2 .) , , . , catch, .

, , , seekBar , null . , , .

+3

, , , ,

     if(mediaPlayer != null & mediaPlayer.isPLaying())
        currentPosition = mediaPlayer.getCurrentPosition();
     else 
        currentPosition = 0;

,

     handler.postDelayed(new Runnable(){
        @Override
        public void run() {
            int runtime = mediaPlayer.getDuration();
            int currentPosition = 0;
            while (currentPosition < runtime) {
               try {
                    if(mediaPlayer != null & mediaPlayer.isPLaying())
                        currentPosition = mediaPlayer.getCurrentPosition();
                    else 
                       currentPosition = 0;    

                    if (seekBar != null) {
                          seekBar.setProgress(currentPosition);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
              }
          }
      }, 500);
+2

, thread.sleep(). , .

, getCurrentPosition() valid:

{Idle, , , , , , PlaybackCompleted}

getCurrentPosition , , isPlaying() .

, Timer TimerTask while while Thread.sleep();

+1

, . , , , , .

. mediaPlayer .

final MediaPlayer mediaPlayer = ...

( . , ):

synchronized (mediaPlayer) {
  if (mediaPlayer != null & mediaPlayer.isPlaying()) {
    currentPosition = mediaPlayer.getCurrentPosition();
  } else {
    currentPosition = 0;
  }
}
+1

, :

You will realize that you must call reset () to return it to the idle state. Only then can you call setDataSource ()

0
source

All Articles