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.