6

I published my app and on some phones like Galaxy Nexus or HTC Explorer where my APP crashes.

I tested on many phones and app worked perfectly.

Can you explain me why is this happening and how to fix it ?

ERROR

java.lang.IllegalStateException
at android.media.MediaPlayer.isPlaying(Native Method)
at com.mario.kvizoman.SoloIgra.novopitanjce(SoloIgra.java:922)
at com.mario.kvizoman.SoloIgra.onClick(SoloIgra.java:901)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3768)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)

CODE

if(reptimer!=null) {
    if(reptimer.isPlaying()) {
        reptimer.stop();
    }
}

onCreate reptimer is

reptimer = MediaPlayer.create(SoloIgra.this, R.raw.napeto);
Michael Celey
  • 12,645
  • 6
  • 57
  • 62
mario
  • 286
  • 1
  • 4
  • 5

2 Answers2

13

According to the Android documentation: "IllegalStateException if the internal player engine has not been initialized or has been released."

So ensure your MediaPlayer is initialized, and you don't use the released one.

Taras
  • 2,526
  • 3
  • 33
  • 63
  • 4
    Hello :) how do I ensure if MediaPlayer is initialized? In my code I have `if(mediaPlayer != null) if(mediaPlayer.isPlaying()) /* do things */`, but I still get the Exception on second `if`. – ocramot Oct 01 '15 at 12:41
  • 3
    @ocramot manually control your player state - e.g. keep the state in local variable or wrap your code with try/catch blocks – Taras Oct 01 '15 at 12:48
  • 1
    Thank you! I was hoping there was some function already built-in in the MediaPlayer library – ocramot Oct 02 '15 at 07:10
6

From your code, since repTimer.isPlaying() is getting invoked, I assume that the JAVA repTimer object is not NULL. However, IllegalStateException is triggered when the native MediaPlayer object is NULL (Reference: http://androidxref.com/4.2.2_r1/xref/frameworks/base/media/jni/android_media_MediaPlayer.cpp#380 ). Hence, there is some mismatch between your JAVA states and native states.

P.S: You may want to check the code prior the position pasted in this question. In some earlier method call, the native object has been destroyed which is not reflected in your JAVA object state.

Ganesh
  • 5,880
  • 2
  • 36
  • 54