MediaPlayer.isplaying()方法中的java.lang.IllegalStateException

前端之家收集整理的这篇文章主要介绍了MediaPlayer.isplaying()方法中的java.lang.IllegalStateException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
public static MediaPlayer mp=null;
public static void playGeneric(int name,final ImageButton button,final ImageButton pervIoUs,Context context) {
    button.setEnabled(false);
button.setClickable(false);
pervIoUs.setEnabled(false);
pervIoUs.setClickable(false);
    try{
        if(mp != null && mp.isPlaying())
        {
            mp.stop();
            mp.release();
            mp = null;
           mp=MediaPlayer.create(context,name);
           mp.start();
        }
        else
        {
            mp = MediaPlayer.create(context,name);
            mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer arg0) {
                    //mp.prepare();
                    mp.start();

                }

            });
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                // TODO Auto-generated method stub
                mp.release();
                System.out.println("Object released");
                button.setEnabled(true);
                button.setClickable(true);
                pervIoUs.setEnabled(true);
                pervIoUs.setClickable(true);
            }
        });
        }



} catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}catch (SecurityException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    e.getMessage();
} catch (NullPointerException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    e.getMessage();
}

}

give me java.lang.IllegalStateException on mp.isplaying() method,i am new in android please help me to solve out.
i want to stop music if its playing and play another song one after another.if anyone have idea let me know thanks in andvance.

logcat :

10-14 15:12:05.474: E/MediaPlayer(15411): prepareAsync called in state 8
10-14 15:12:05.474: W/System.err(15411): java.lang.IllegalStateException
10-14 15:12:05.474: W/System.err(15411):    at android.media.MediaPlayer.prepare(Native Method)
10-14 15:12:05.474: W/System.err(15411):    at com.rogerscenter.LearnReadWriteSpell.Utility.Music.playGeneric(Music.java:93)
10-14 15:12:05.474: W/System.err(15411):    at com.rogerscenter.LearnReadWriteSpell.LetterCategory.Letter_Lesson1_activity.onCreate(Letter_Lesson1_activity.java:140)
10-14 15:12:05.474: W/System.err(15411):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-14 15:12:05.474: W/System.err(15411):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
10-14 15:12:05.474: W/System.err(15411):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
10-14 15:12:05.474: W/System.err(15411):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-14 15:12:05.474: W/System.err(15411):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
10-14 15:12:05.484: W/System.err(15411):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 15:12:05.484: W/System.err(15411):    at android.os.Looper.loop(Looper.java:123)
10-14 15:12:05.484: W/System.err(15411):    at android.app.ActivityThread.main(ActivityThread.java:3647)
10-14 15:12:05.484: W/System.err(15411):    at java.lang.reflect.Method.invokeNative(Native Method)
10-14 15:12:05.484: W/System.err(15411):    at java.lang.reflect.Method.invoke(Method.java:507)
10-14 15:12:05.484: W/System.err(15411):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-14 15:12:05.484: W/System.err(15411):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-14 15:12:05.484: W/System.err(15411):    at dalvik.system.NativeStart.main(Native Method)

解决方法

由于android文档建议,如果mp是如果没有被初始化,那么java.lang.IllegalStateException将被抛出,所以你必须首先启动,或者你必须写

查看文档http://developer.android.com/reference/android/media/MediaPlayer.html#isPlaying()

尝试这样

mp=MediaPlayer.create(context,name);

     try {

    if (mp.isPlaying()) {
        mp.stop();
        mp.release();
        mp=MediaPlayer.create(context,name);
    }



    mp.start();
} catch (Exception e) {
}

猜你在找的Java相关文章