MediaPlayer. Java. In the displaying () method lang.IllegalStateException
•
Java
public static MediaPlayer mp=null;
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(); }
}
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)
Solution
As the Android documentation suggests, if MP is not initialized, then Java Lang. IllegalStateException will be thrown, so you must start first, or you must write
View document http://developer.android.com/reference/android/media/MediaPlayer.html#isPlaying ()
Try this
mp=MediaPlayer.create(context,name); try { if (mp.isPlaying()) { mp.stop(); mp.release(); mp=MediaPlayer.create(context,name); } mp.start(); } catch (Exception e) { }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码