gpt4 book ai didi

android - mp.pause();使应用程序崩溃

转载 作者:行者123 更新时间:2023-12-03 02:12:38 28 4
gpt4 key购买 nike

试图在我的应用程序中放置一个“暂停”按钮,以播放一些声音片段循环播放。

当我打电话mp.pause();一切都破了,我完全迷路了!

这是我正在使用的方法。

    protected void managerOfSound(String theText) {
if (mp != null) {
mp.reset();
mp.release();
}

if (theText.equals(campfire))
mp = MediaPlayer.create(this, R.raw.campfire);
else if (theText.equals(calmthunder))
mp = MediaPlayer.create(this, R.raw.calmthunderstorm);
else if (theText.equals(rainthunder))
mp = MediaPlayer.create(this, R.raw.rainthunder);
else if (theText.equals(whalesgulls))
mp = MediaPlayer.create(this, R.raw.whalesandgulls);
else if (theText.equals(stopplaying))
mp.pause();

mp.start();
mp.setLooping(true);
}

这是一个logcat(喵^ _ ^)
threadid=1: thread exiting with uncaught exception (group=0xb2f6c648)
FATAL EXCEPTION: main
java.lang.IllegalStateException
at android.media.MediaPlayer.setLooping(Native Method)
at com.tags4apps.soothingsounds.MainActivity.managerOfSound(MainActivity.java:83)
at com.tags4apps.soothingsounds.MainActivity$1.onClick(MainActivity.java:34)
android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

我现在愿意接受任何建议

编辑:反对票是什么,我现在是晚上7点。自下午3点以来,我一直在寻找解决方法。所以说它缺乏研究是不公平的。

最佳答案

这可能不是完整的答案,但是我将尝试在当前代码中解决一个相当明显的问题:

protected void managerOfSound(String theText) {
if (mp != null) {
mp.reset();
mp.release(); //from this point on it is illegal
//to operate on the existing mp object
}
//if you take one of these branches you will get a new mp instance, and be fine
if (theText.equals(campfire))
mp = MediaPlayer.create(this, R.raw.campfire);
else if (theText.equals(calmthunder))
mp = MediaPlayer.create(this, R.raw.calmthunderstorm);
else if (theText.equals(rainthunder))
mp = MediaPlayer.create(this, R.raw.rainthunder);
else if (theText.equals(whalesgulls))
mp = MediaPlayer.create(this, R.raw.whalesandgulls);
//but if you take this branch, you will be illegally operating on a released instance
else if (theText.equals(stopplaying))
mp.pause();

mp.start();
mp.setLooping(true);
}

相反,请执行以下操作:
protected void managerOfSound(String theText) {
if (theText.equals(stopplaying)) {
if (mp != null) mp.pause();

} else { //we are not pausing
if (mp != null) { //lets release any old one
mp.reset();
mp.release();
}
//and then get an appropriate new instance
if (theText.equals(campfire))
mp = MediaPlayer.create(this, R.raw.campfire);
else if (theText.equals(calmthunder))
mp = MediaPlayer.create(this, R.raw.calmthunderstorm);
else if (theText.equals(rainthunder))
mp = MediaPlayer.create(this, R.raw.rainthunder);
else if (theText.equals(whalesgulls))
mp = MediaPlayer.create(this, R.raw.whalesandgulls);
//Note this will still fail if there is some OTHER possibility
mp.start();
mp.setLooping(true);
}
}

关于android - mp.pause();使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22793230/

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