Search code examples
javaandroidmedia-playerillegalstateexception

java.lang.IllegalStateException in MediaPlayer


This is my code:

final MediaPlayer[] threeSound = new MediaPlayer[1];
threeSound[0] = new MediaPlayer();
final CountDownTimer playThreeSound = new CountDownTimer(1000, 1) {
    boolean timerStarted = false;
    @Override
    public void onTick(long millisUntilFinished) {
        torgText.setText("3...");
        if (!timerStarted) {
            timerStarted = true;
            threeSound[0] = MediaPlayer.create(PlayActivity.this, R.raw.three);
            try {
                threeSound[0].prepare();
                threeSound[0].start();
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("IOE", "Something went wrong");
            }
        }
    }

    @Override
    public void onFinish() {
        if (threeSound[0].isPlaying()) {
            threeSound[0].stop();
        }
        playTwoSound.start();
    }
};

It throws IllegalStateException. These are logs:

FATAL EXCEPTION: main
Process: testapplication.android.com.guesstune_v2, PID: 3641
java.lang.IllegalStateException
at android.media.MediaPlayer._prepare(Native Method)
at android.media.MediaPlayer.prepare(MediaPlayer.java:1351)
at testapplication.android.com.guesstune_v2.PlayActivity$6.onTick(PlayActivity.java:316)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:133)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:7007)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

What is wrong with preparing MediaPlayer? What should I add to the code? I'm a newbie, sorry for a probably stupid question and for bad English.


Solution

  • The documentation for MediaPlayer states:

    MediaPlayer create (Context context, int resid) Convenience method to create a MediaPlayer for a given resource id. On success, prepare() will already have been called and must not be called again.

    https://developer.android.com/reference/android/media/MediaPlayer.html#create(android.content.Context, int)

    So your IllegalStateException occurs because prepare() requires the MediaPlayer to be in either a Initialized or Stopped state, but when create(Context context, int resid) is invoked, it calls prepare() resulting in the MediaPlayer being in the Prepared state which it must not be when prepare() is invoked.

    In short: remove the prepare() call and the IllegalStateException should no longer occur.

    A full State Diagram and list of valid states is presented in the documentation.