gpt4 book ai didi

调用 pause() 时 Android MediaPlayer IllegalStateExceptoion

转载 作者:行者123 更新时间:2023-11-29 02:37:29 27 4
gpt4 key购买 nike

我想根据应用程序的状态播放两个不同的背景音乐循环。为此,我尝试了该代码:

private void backgroundMusicPlayer() {
if (gameMode == 0) {
if (backgroundloop2 != null) {
backgroundloop2.pause();
backgroundloop2.stop();
backgroundloop2.release();
backgroundloop2 = null;
}
backgroundloop1 = MediaPlayer.create(getContext(), R.raw.gameloop1);
backgroundloop1.setLooping(true);
backgroundloop1.start();
}
else {
if (backgroundloop1 != null) {
backgroundloop1.pause();
backgroundloop1.stop();
backgroundloop1.release();
backgroundloop1 = null;
}
backgroundloop2 = MediaPlayer.create(getContext(), R.raw.gameloop2);
backgroundloop2.setLooping(true);
backgroundloop2.start();
}
}

但我只是得到错误:

"MediaPlayer: start called in state 64" "MediaPlayer: pause called in state 8" "Failed to open libwvm.so: dlopen failed: library "libwvm.so" not found" "Media Player called in state 0, error (-38,0)"

我怎样才能正确地做到这一点?

最佳答案

为什么在 onPause() 中出现 IllegalStateException?

onPause 的 Android 文档中:

IllegalStateException
If the internal player engine has not been initialized.

在文档中,您可以看到最多的媒体播放器方法会出于某种原因抛出 IllegalStateException。

因此,对所有媒体播放器操作使用try catch

Android 建议在使用媒体播放器对象时查找异常。

It is good programming practice to always look out for IllegalArgumentException and IOException that may be thrown from the overloaded setDataSource methods.

关于调用 pause() 时 Android MediaPlayer IllegalStateExceptoion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46223304/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com