java.lang.IllegalStateException in MediaPlayer.isplaying() method

前端 未结 3 1326
夕颜
夕颜 2021-02-12 10:30
public static MediaPlayer mp=null;
public static void playGeneric(int name, final ImageButton button,final ImageButton pervious,Context context) {
    button.setEnabled(         


        
3条回答
  •  眼角桃花
    2021-02-12 11:16

    As android docs suggest that if mp is if has not been initialized at that time java.lang.IllegalStateException will be thrown so you have to initilize first or you have to write

    check out the docs http://developer.android.com/reference/android/media/MediaPlayer.html#isPlaying()

    try like this

     mp=MediaPlayer.create(context, name);
    
         try {
    
        if (mp.isPlaying()) {
            mp.stop();
            mp.release();
            mp=MediaPlayer.create(context, name);
        }
    
    
    
        mp.start();
    } catch (Exception e) {
    }
    

提交回复
热议问题