Android – this error java.lang.IllegalStateException at MediaPlayer.getCurrentPosition

androidmedia-player

The error is :

05-06 22:11:15.041: E/AndroidRuntime(15780): FATAL EXCEPTION: main
05-06 22:11:15.041: E/AndroidRuntime(15780): java.lang.IllegalStateException
05-06 22:11:15.041: E/AndroidRuntime(15780):    at android.media.MediaPlayer.getCurrentPosition(Native Method)
05-06 22:11:15.041: E/AndroidRuntime(15780):    at android.azher.qrcodespeech.SearchActivity$2.onClick(SearchActivity.java:109)
05-06 22:11:15.041: E/AndroidRuntime(15780):    at android.view.View.performClick(View.java:4204)
05-06 22:11:15.041: E/AndroidRuntime(15780):    at android.view.View$PerformClick.run(View.java:17355)
05-06 22:11:15.041: E/AndroidRuntime(15780):    at android.os.Handler.handleCallback(Handler.java:725)
05-06 22:11:15.041: E/AndroidRuntime(15780):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-06 22:11:15.041: E/AndroidRuntime(15780):    at android.os.Looper.loop(Looper.java:137)
05-06 22:11:15.041: E/AndroidRuntime(15780):    at android.app.ActivityThread.main(ActivityThread.java:5041)
05-06 22:11:15.041: E/AndroidRuntime(15780):    at java.lang.reflect.Method.invokeNative(Native Method)
05-06 22:11:15.041: E/AndroidRuntime(15780):    at java.lang.reflect.Method.invoke(Method.java:511)
05-06 22:11:15.041: E/AndroidRuntime(15780):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-06 22:11:15.041: E/AndroidRuntime(15780):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-06 22:11:15.041: E/AndroidRuntime(15780):    at dalvik.system.NativeStart.main(Native Method)

The code where I got this error
(exactly in this line int currentPosition = mp.getCurrentPosition();

):

mp = new MediaPlayer();
    try {
        mp.prepare();
    } catch (IllegalStateException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


    ////---- play the speech ---------------------------------------------------------
    btnPlay.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
            File appTmpPath = new File(exStoragePath + "/QRSpeech/sounds/");
            appTmpPath.mkdirs();
            String tempFilename = bookName.replace(" ", "_")+"_"+pageIndex+".3gpp"; 
            String tempDestFile = appTmpPath.getAbsolutePath()  +"/" +tempFilename;
            Log.d("path >>>>>>> ", tempDestFile);
            try {
                mp.reset();
                FileInputStream fis = new FileInputStream(tempDestFile);
                mp.setDataSource(fis.getFD());

                if( pauseTime > 0)
                    mp.seekTo(pauseTime);
                mp.start();

            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                mp.stop();
                mp.release();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                mp.stop();
                mp.release();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                mp.stop();
                mp.release();
            }
            int currentPosition = mp.getCurrentPosition();
            int total = mp.getDuration();

            sb.setMax(total); // Set the Maximum range of the
            sb.setProgress(currentPosition);// set current progress to song's

            handler.removeCallbacks(moveSeekBarThread);
            handler.postDelayed(moveSeekBarThread, 100); //cal the thread after 100 milliseconds
        }
    });

Best Solution

you get this error, if you call a function of the Mediaplayer, and the MediaPlayer is in wrong State.For example, if your MediaPlayer is in the Initialized-State, you cannot call start(), so you have to wait, till your MediaPlayer is in the Prepared-State. Check the StateDiagram at http://developer.android.com/reference/android/media/MediaPlayer.html

In your try block, you release() the Mediaplayer, than you start() it (without call prepare() first). Usually you listen to the PreparedState and than call start.

You can only seek the Mediaplayer in Prepared / Started or Paused State