MediaPlayer IllegalStateException

时间:2016-03-21 16:05:32

标签: android android-mediaplayer

我创建了一个初始化mediaplayer的方法,并在onCreate()中声明了它;

MediaPlayer sixty,fifty,forty, thirty, twenty, fifteen, ten, five;
MediaPlayer mp1[] = {sixty,fifty,forty,thirty,twenty,fifteen,ten,five,warning};
String path[] = {"sixty","fifty","forty", "thirty", "twenty", "fifteen", "ten", "five", "warning"};`
public void InitMusic() {
    for(int i = 0; i < mp1.length; i++) {
        try {
            mp1[i] = new MediaPlayer();
            Uri ey = Uri.parse("android.resource//" + this.getPackageName() + "/" + path[i]);

            mp1[i].setDataSource(MainActivity.this, ey);
            mp1[i].setAudioStreamType(AudioManager.STREAM_MUSIC);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

也是每次我想再次使用它时准备音乐的方法

public void MusicPrepStart(int i) {
        mp1[i].prepareAsync();
        MusicPrepListeners(i);
}

public void MusicPrepListeners(int i) {
    switch(i){
        case 0:
            mp1[0].setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp1[0].start();
                    mp1[0].setLooping(true);
                }
            });
            break;
        case 1:
            mp1[1].setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp1[1].start();
                    mp1[1].setLooping(true);
                }
            });
            break;
        case 2:
            mp1[2].setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp1[2].start();
                    mp1[2].setLooping(true);
                }
            });
            break;
        case 3:
            mp1[3].setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp1[3].start();
                    mp1[3].setLooping(true);
                }
            });
            break;
        case 4:
            mp1[4].setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp1[4].start();
                    mp1[4].setLooping(true);
                }
            });
            break;
        case 5:
            mp1[5].setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp1[5].start();
                    mp1[5].setLooping(true);
                }
            });
            break;
        case 6:
            mp1[6].setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp1[6].start();
                    mp1[6].setLooping(true);
                }
            });
            break;
        case 7:
            mp1[7].setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp1[7].start();
                    mp1[7].setLooping(true);
                }
            });
            break;
        case 8:
            mp1[8].setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp1[8].start();
                    mp1[8].setLooping(true);
                }
            });
            break;
    }
}
然后我就像这样使用这个代码。当我从70-80收到arduino中的数据时,mp [8]应该准备好并启动但是它会给出一个illegalStateException错误。

if (distance<=80 && distance > 70 && entered == false) {                                    
    MusicPrepStart(8);
}

logcat的

FATAL EXCEPTION: main
java.lang.IllegalStateException
android.media.MediaPlayer.prepareAsync(Native Method)
at com.ardudroid.projects.ultrasoundbelt.MainActivity.MusicPrepStart(MainActivity.java:536)
at com.ardudroid.projects.ultrasoundbelt.MainActivity$47$1.run(MainActivity.java:1622)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5071)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)

MainActivity.java:536mp1[i].prepareAsync(); MainActivity.java:1622MusicPrepStart(8),我在这里实施媒体播放器有什么问题,请帮忙。

1 个答案:

答案 0 :(得分:0)

数据正在流式传输,因此它继续调用MusicPrepStart(8)

  

抛出IllegalStateException以防止编程错误,例如调用prepare(),prepareAsync()或其中一个重载的setDataSource方法处于无效状态。