MediaPlayer.isplaying()方法中的java.lang.IllegalStateException

时间:2013-10-14 09:19:23

标签: java android android-mediaplayer

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();
    }

}

在mp.isplaying()方法上给我java.lang.IllegalStateException。

我想停止音乐,如果它正在播放并且一个接一个地播放另一首歌。

logcat的:

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)

4 个答案:

答案 0 :(得分:9)

尝试将mp.release()更改为reset()。这可以帮到你。

答案 1 :(得分:8)

由于android文档提示如果mp当时尚未初始化,则会抛出java.lang.IllegalStateException,因此您必须首先初始化或者必须编写

查看文档http://developer.android.com/reference/android/media/MediaPlayer.html#isPlaying()

试试这个

 mp=MediaPlayer.create(context, name);

     try {

    if (mp.isPlaying()) {
        mp.stop();
        mp.release();
        mp=MediaPlayer.create(context, name);
    }



    mp.start();
} catch (Exception e) {
}

答案 2 :(得分:0)

好的,我在这里提出了我的解决方案,希望它可以帮助其他人。

(i) 如果你使用的是 videoView 和 mediaPlayer.isPlaying() (isNative) 会发生错误 活动停止时

添加这个 onStop() 方法:

    if (videoView != null && videoView.isPlaying()) {
        videoView.pause();
        videoView.stopPlayback();
    }

注意:在使用 videoView 时不要在这里使用 mediaPlayer.stop() 或 mediaPlayer.release(),它会抛出 IllegalException

(ii) 如果您使用 mediaPlayer 仅播放音频

添加这个 onStop() 方法:

   try {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    } catch (IllegalStateException e) {
        e.printStackTrace();
    }

注意:这仅适用于在您的活动处于停止或恢复状态时出错的人。

答案 3 :(得分:-1)

使用runOnUiThread进行mediaRecorder准备。

private boolean prepareMediaRecorder() {

    mediaRecorder = new MediaRecorder();

    runOnUiThread(new Runnable() {
        @Override
        public void run() {

            mediaRecorder.reset();
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);

            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

            mediaRecorder.setOutputFile(filePath);

            try {
                mediaRecorder.prepare();
            } catch (IOException e) {
                mediaRecorder = null;
                return;
            }
            mediaRecorder.start();
            recording = true;
        }
    });

    return true;
}