MediaPlayer.getCurrentPosition上的此错误java.lang.IllegalStateException是什么 [英] What is this error java.lang.IllegalStateException at MediaPlayer.getCurrentPosition

查看:368
本文介绍了MediaPlayer.getCurrentPosition上的此错误java.lang.IllegalStateException是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误是:

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();

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
        }
    });

推荐答案

如果调用Mediaplayer的函数,并且MediaPlayer处于错误状态,例如,如果MediaPlayer处于,您无法调用start(),因此您必须等待,直到MediaPlayer位于Prepared-State中.在 http://developer.android.com/reference/android/media中查看StateDiagram /MediaPlayer.html

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

在您的try块中,您的媒体播放器是release(),而不是start()(首先不调用prepare()).通常,您会监听PreparedState并调用start.

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.

您只能在准备/开始或暂停状态下搜索Mediaplayer

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

这篇关于MediaPlayer.getCurrentPosition上的此错误java.lang.IllegalStateException是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆