IllegalStateException throws MediaPlayer.reset ()

Android MediaPlayer documentation shows that there are no invalid states for reset() : http://developer.android.com/reference/android/media/MediaPlayer.html#Valid_and_Invalid_States (invalid states are listed as {} or "none.") .

However, I saw how an IllegalStateException is IllegalStateException when reset() called:

 java.lang.IllegalStateException at android.media.MediaPlayer._reset(Native Method) at android.media.MediaPlayer.reset(MediaPlayer.java:1061) at com.example.android.player.AsyncPlayer$AsyncHandler.handleMessage(AsyncPlayer.java:654) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.os.HandlerThread.run(HandlerThread.java:60) 

Is the documentation incorrect?

+16
source share
3 answers

It's hard to say without seeing your code, but I think you can call reset () after calling release ()?

The documentation states

When a MediaPlayer object is only created using a new one or after reset () is called, it is in a pending state; and after calling release () it is in End state. Between these two states is the life cycle of the MediaPlayer.

You can call reset outside the actual life cycle.

+20
source

I ran into your problem, Skyler.

You're right. There are no invalid states in the documentation for mediaPlayer.reset (), but this is not the first inaccuracy in the documentation.

I noticed that the VALID state list does not say "Any"; he lists each specific state, except for two: "Preparation and completion."

I experimented, but could not get an IllegalStateException to be thrown in my attempts to call release (), while MediaPlayer was hoping in a prepared state (using prepareAsync ()). I will not guarantee that this will not happen, but I could not do it. In this case, I saw the following log messages:

 04-11 11:41:54.740: E/MediaPlayer(4930): error (1, -2147483648) 04-11 11:41:54.748: E/MediaPlayer(4930): Error (1,-2147483648) 

Yes, both error messages appear, one after the other - one with a lowercase โ€œerrorโ€ and one with an uppercase โ€œErrorโ€, but no Exception is thrown.

However, if I call reset () after release (), I get an error:

 04-11 11:45:05.232: E/AndroidRuntime(5046): FATAL EXCEPTION: main 04-11 11:45:05.232: E/AndroidRuntime(5046): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.helloandroid/com.android.helloandroid.HelloAndroidActivity}: java.lang.RuntimeException: java.lang.IllegalStateException 04-11 11:45:05.232: E/AndroidRuntime(5046): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1696) 04-11 11:45:05.232: E/AndroidRuntime(5046): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716) 04-11 11:45:05.232: E/AndroidRuntime(5046): at android.app.ActivityThread.access$1500(ActivityThread.java:124) 04-11 11:45:05.232: E/AndroidRuntime(5046): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968) 04-11 11:45:05.232: E/AndroidRuntime(5046): at android.os.Handler.dispatchMessage(Handler.java:99) 04-11 11:45:05.232: E/AndroidRuntime(5046): at android.os.Looper.loop(Looper.java:123) 04-11 11:45:05.232: E/AndroidRuntime(5046): at android.app.ActivityThread.main(ActivityThread.java:3806) 04-11 11:45:05.232: E/AndroidRuntime(5046): at java.lang.reflect.Method.invokeNative(Native Method) 04-11 11:45:05.232: E/AndroidRuntime(5046): at java.lang.reflect.Method.invoke(Method.java:507) 04-11 11:45:05.232: E/AndroidRuntime(5046): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 04-11 11:45:05.232: E/AndroidRuntime(5046): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 04-11 11:45:05.232: E/AndroidRuntime(5046): at dalvik.system.NativeStart.main(Native Method) 04-11 11:45:05.232: E/AndroidRuntime(5046): Caused by: java.lang.RuntimeException: java.lang.IllegalStateException 04-11 11:45:05.232: E/AndroidRuntime(5046): at com.android.helloandroid.HelloAndroidActivity.crashMediaPlayer(HelloAndroidActivity.java:423) 04-11 11:45:05.232: E/AndroidRuntime(5046): at com.android.helloandroid.HelloAndroidActivity.onCreate(HelloAndroidActivity.java:87) 04-11 11:45:05.232: E/AndroidRuntime(5046): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 04-11 11:45:05.232: E/AndroidRuntime(5046): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660) 04-11 11:45:05.232: E/AndroidRuntime(5046): ... 11 more 04-11 11:45:05.232: E/AndroidRuntime(5046): Caused by: java.lang.IllegalStateException 04-11 11:45:05.232: E/AndroidRuntime(5046): at android.media.MediaPlayer._reset(Native Method) 04-11 11:45:05.232: E/AndroidRuntime(5046): at android.media.MediaPlayer.reset(MediaPlayer.java:1112) 04-11 11:45:05.232: E/AndroidRuntime(5046): at com.android.helloandroid.HelloAndroidActivity.crashMediaPlayer(HelloAndroidActivity.java:421) 04-11 11:45:05.232: E/AndroidRuntime(5046): ... 14 more 

So self-improvement of Ink Ink was right. MediaPlayer.reset () throws an IllegalStateException in the End state (which occurs after the release () call).

In my case, I found that I was calling release () on onPause (), but did nothing to initialize MediaPlayer again in onResume (). Therefore, it was in End state when I called reset ();

Per http://developer.android.com/reference/android/media/MediaPlayer.html ,

When the MediaPlayer object is in the End state, it can no longer be and there is no way to return it to another state.

This means that you need to create MediaPlayer again, starting with mediaPlayer = new MediaPlayer () or one of the mediaPlayer.onCreate () methods. Or be careful when you call release ().

+8
source

Apparently, the documentation for Android MediaPlayer is incorrect regarding the invalid state for reset() . Below is what will happen when I experienced this:

In my PlayerActivity.java code PlayerActivity.java I set my MediaPlayer as static in order to use it in my home activity :

 public class PlayerActivity extends Activity { .... public static MediaPlayer mp; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Mediaplayer if(mp == null) { mp = new MediaPlayer(); } .... } /** * Function to play a song * @param songIndex - index of song * */ public void playSong(int songIndex){ // Play song try { if(mUpdateTimeTask != null) mHandler.removeCallbacks(mUpdateTimeTask); mp.reset(); // the song path is get from internet mp.setDataSource(songsList.get(songIndex).get("songPath")); mp.prepareAsync(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } ... } 

In my activity house, I release the player before closing the application:

 public class TuoiTreAppActivity extends TabActivity { ... @Override public void onDestroy(){ if(PlayerActivity.mp != null) { PlayerActivity.mp.release(); } super.onDestroy(); } ... } 

So, when I launch the application for the first time and start playing a song. The reset() function works without errors. But when I hit the button back to close the application and run it a second time, an IllegalStateException occurs when passing the reset() function.

I also found a reason when debugging. When the application is first launched, the player is null, so it is initialized with the onCreate() PlayerActivity.java . But the player is not freed until null after closing the application. Therefore, it is not initialized again when it is reopened a second time. The reason IllegalStateException occurs when passing the reset() function. So, to solve this problem, I have to install the null player before closing the application:

 @Override public void onDestroy(){ if(PlayerActivity.mp != null) { PlayerActivity.mp.release(); // Set the MediaPlayer to null to avoid IlLegalStateException // when call mp.reset() after launching the app again PlayerActivity.mp = null; } super.onDestroy(); } 
+3
source

All Articles