web-dev-qa-db-fra.com

java.lang.IllegalStateException dans la méthode MediaPlayer.isplaying ()

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

}

donnez-moi Java.lang.IllegalStateException sur la méthode mp.isplaying ().

Je veux arrêter la musique si elle joue et jouer une autre chanson l'une après l'autre.

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)
18
Sydroid

Essayez de changer mp.release() en reset(). cela pourrait vous aider.

12
Mester Hassan

Comme Android suggèrent que si mp est si n'a pas été initialisé à ce moment-là, Java.lang.IllegalStateException sera levée, vous devez donc l'initialiser en premier ou vous devez écrire

consultez les documents http://developer.Android.com/reference/Android/media/MediaPlayer.html#isPlaying ()

essayez comme ça

 mp=MediaPlayer.create(context, name);

     try {

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



    mp.start();
} catch (Exception e) {
}
9
Siddhpura Amit