What is this java.lang.IllegalStateException error in MediaPlayer.getCurrentPosition

Error:

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)

Code where I got this error (in this line exactly 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
        }
    });
0
source share
1 answer

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

try release() , start() ( prepare() ). PreparedState .

"" / "" ""

+2

All Articles