0

错误是:

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)

我得到这个错误的代码(正好在这一行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
        }
    });
4

1 回答 1

2

如果你调用了MediaPlayer的一个函数,而MediaPlayer处于错误的状态,你会得到这个错误。例如,如果你的MediaPlayer在Initialized-State,你不能调用start(),所以你必须等待,直到你的MediaPlayer在Prepared-State在http://developer.android.com/reference/android/media/MediaPlayer.html检查 StateDiagram

在您的尝试块中,您release()是 Mediaplayer,而不是您start()(无需prepare()先调用)。通常你听 PreparedState 然后调用 start。

您只能在 Prepared / Started 或 Paused 状态下寻找 Mediaplayer

于 2013-05-06T19:29:26.317 回答