我正在制作一个基本课程,使用一个按钮播放声音,并使用不同的按钮移动到下一个屏幕。问题是,使用声音按钮后,下一个按钮会崩溃应用程序。我以为是因为我发布声音然后检查它,但它仍然崩溃。
public class Explain1 extends Activity
{
MediaPlayer mysound;
protected boolean active = true;
protected int splashtime = 17000;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.exp1);
mysound=MediaPlayer.create(Explain1.this, R.raw.ex1 );
}
@Override
protected void onDestroy()
{
super.onDestroy();
mysound.stop();
mysound.reset();
mysound.release();
mysound = null;
}
public void cont(View view)
{
if(mysound.isPlaying())
{
mysound.stop();
mysound.reset();
mysound.release();
Log.d("Exercise", "Sound Stopped");
}
Intent i = new Intent();
i.setClassName("com.example","com.example.timestableseasy.Explain2");
startActivity(i);
}
public void listen(View view)
{
mysound=MediaPlayer.create(Explain1.this, R.raw.ex1 );
mysound.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mysound)
{
mysound.stop();
mysound.release();
mysound=MediaPlayer.create(Explain1.this, R.raw.ex1 );
Log.d("Exercise", "Sound PLayed");
}
});
mysound.start();
}
}
这是日志猫。
06-10 14:35:39.444: E/AndroidRuntime(30611): FATAL EXCEPTION: main
06-10 14:35:39.444: E/AndroidRuntime(30611): java.lang.IllegalStateException: Could not execute method of the activity
06-10 14:35:39.444: E/AndroidRuntime(30611): at android.view.View$1.onClick(View.java:3814)
06-10 14:35:39.444: E/AndroidRuntime(30611): at android.view.View.performClick(View.java:4421)
06-10 14:35:39.444: E/AndroidRuntime(30611): at android.view.View$PerformClick.run(View.java:17903)
06-10 14:35:39.444: E/AndroidRuntime(30611): at android.os.Handler.handleCallback(Handler.java:730)
06-10 14:35:39.444: E/AndroidRuntime(30611): at android.os.Handler.dispatchMessage(Handler.java:92)
06-10 14:35:39.444: E/AndroidRuntime(30611): at android.os.Looper.loop(Looper.java:213)
06-10 14:35:39.444: E/AndroidRuntime(30611): at android.app.ActivityThread.main(ActivityThread.java:5225)
06-10 14:35:39.444: E/AndroidRuntime(30611): at java.lang.reflect.Method.invokeNative(Native Method)
06-10 14:35:39.444: E/AndroidRuntime(30611): at java.lang.reflect.Method.invoke(Method.java:525)
06-10 14:35:39.444: E/AndroidRuntime(30611): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
06-10 14:35:39.444: E/AndroidRuntime(30611): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-10 14:35:39.444: E/AndroidRuntime(30611): at dalvik.system.NativeStart.main(Native Method)
06-10 14:35:39.444: E/AndroidRuntime(30611): Caused by: java.lang.reflect.InvocationTargetException
06-10 14:35:39.444: E/AndroidRuntime(30611): at java.lang.reflect.Method.invokeNative(Native Method)
06-10 14:35:39.444: E/AndroidRuntime(30611): at java.lang.reflect.Method.invoke(Method.java:525)
06-10 14:35:39.444: E/AndroidRuntime(30611): at android.view.View$1.onClick(View.java:3809)
06-10 14:35:39.444: E/AndroidRuntime(30611): ... 11 more
06-10 14:35:39.444: E/AndroidRuntime(30611): Caused by: java.lang.IllegalStateException
06-10 14:35:39.444: E/AndroidRuntime(30611): at android.media.MediaPlayer.isPlaying(Native Method)
06-10 14:35:39.444: E/AndroidRuntime(30611): at com.example.timestableseasy.Explain1.cont(Explain1.java:38)
06-10 14:35:39.444: E/AndroidRuntime(30611): ... 14 more
这可能很简单,但我看不出问题出在哪里。
答案 0 :(得分:1)
您不应release
MediaPlayer
的连接。足够的事情是reset
。
release(): 此方法释放随MediaPlayer对象附加的所有资源
重置()强>: 此方法重置媒体播放器
你需要做的是
mp.reset();
mp.setDataSource(/*Code to provide data source*/);
mp.prepare();
mp.start();
请检查此Media Player Example。