java.lang.IllegalStateException 在媒体播放器中
java.lang.IllegalStateException in MediaPlayer
这是我的代码:
final MediaPlayer[] threeSound = new MediaPlayer[1];
threeSound[0] = new MediaPlayer();
final CountDownTimer playThreeSound = new CountDownTimer(1000, 1) {
boolean timerStarted = false;
@Override
public void onTick(long millisUntilFinished) {
torgText.setText("3...");
if (!timerStarted) {
timerStarted = true;
threeSound[0] = MediaPlayer.create(PlayActivity.this, R.raw.three);
try {
threeSound[0].prepare();
threeSound[0].start();
} catch (IOException e) {
e.printStackTrace();
Log.e("IOE", "Something went wrong");
}
}
}
@Override
public void onFinish() {
if (threeSound[0].isPlaying()) {
threeSound[0].stop();
}
playTwoSound.start();
}
};
它抛出 IllegalStateException。这些是日志:
FATAL EXCEPTION: main
Process: testapplication.android.com.guesstune_v2, PID: 3641
java.lang.IllegalStateException
at android.media.MediaPlayer._prepare(Native Method)
at android.media.MediaPlayer.prepare(MediaPlayer.java:1351)
at testapplication.android.com.guesstune_v2.PlayActivity.onTick(PlayActivity.java:316)
at android.os.CountDownTimer.handleMessage(CountDownTimer.java:133)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:7007)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
准备MediaPlayer有什么问题?我应该在代码中添加什么?我是新手,很抱歉问了一个愚蠢的问题和糟糕的英语。
MediaPlayer
的文档指出:
MediaPlayer create (Context context, int resid)
Convenience method to create a MediaPlayer for a given resource id. On success, prepare()
will already have been called and must not be called again.
所以你的 IllegalStateException
发生是因为 prepare()
要求 MediaPlayer
处于 Initialized 或 Stopped 状态,但是当 create(Context context, int resid)
被调用时,它调用 prepare()
导致 MediaPlayer
处于 Prepared 状态,它不能在调用 prepare()
时。
简而言之:删除 prepare()
调用,IllegalStateException
不应再发生。
文档中提供了完整的状态图和有效状态列表。
这是我的代码:
final MediaPlayer[] threeSound = new MediaPlayer[1];
threeSound[0] = new MediaPlayer();
final CountDownTimer playThreeSound = new CountDownTimer(1000, 1) {
boolean timerStarted = false;
@Override
public void onTick(long millisUntilFinished) {
torgText.setText("3...");
if (!timerStarted) {
timerStarted = true;
threeSound[0] = MediaPlayer.create(PlayActivity.this, R.raw.three);
try {
threeSound[0].prepare();
threeSound[0].start();
} catch (IOException e) {
e.printStackTrace();
Log.e("IOE", "Something went wrong");
}
}
}
@Override
public void onFinish() {
if (threeSound[0].isPlaying()) {
threeSound[0].stop();
}
playTwoSound.start();
}
};
它抛出 IllegalStateException。这些是日志:
FATAL EXCEPTION: main
Process: testapplication.android.com.guesstune_v2, PID: 3641
java.lang.IllegalStateException
at android.media.MediaPlayer._prepare(Native Method)
at android.media.MediaPlayer.prepare(MediaPlayer.java:1351)
at testapplication.android.com.guesstune_v2.PlayActivity.onTick(PlayActivity.java:316)
at android.os.CountDownTimer.handleMessage(CountDownTimer.java:133)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:7007)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
准备MediaPlayer有什么问题?我应该在代码中添加什么?我是新手,很抱歉问了一个愚蠢的问题和糟糕的英语。
MediaPlayer
的文档指出:
MediaPlayer create (Context context, int resid)
Convenience method to create a MediaPlayer for a given resource id. On success,prepare()
will already have been called and must not be called again.
所以你的 IllegalStateException
发生是因为 prepare()
要求 MediaPlayer
处于 Initialized 或 Stopped 状态,但是当 create(Context context, int resid)
被调用时,它调用 prepare()
导致 MediaPlayer
处于 Prepared 状态,它不能在调用 prepare()
时。
简而言之:删除 prepare()
调用,IllegalStateException
不应再发生。
文档中提供了完整的状态图和有效状态列表。