Android sound problem
In my app when user clicks a button, a sound is played. Since the user can push that button many times and much faster than the length of the sound, i shortened the sound. Now it's like 0.2 sec or sg. Strange is, now Android cannot play the sound, my app force closes. So is it the length of the sound or what? It is a similar .mp3 than the one before. This is the code i am using:
mp = MediaPlayer.create(Start.this, R.raw.clicksound2);
mp.start();
NameButton.setEnabled(false);
mp.setOnCompletionListener(n开发者_JS百科ew OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
NameButton.setEnabled(true);
}
});
Logcat:
06-09 05:38:23.774: ERROR/AndroidRuntime(551): java.lang.NullPointerException
06-09 05:38:23.774: ERROR/AndroidRuntime(551): at com.bfarago.clickme.Start$1.onClick(Start.java:113)
06-09 05:38:23.774: ERROR/AndroidRuntime(551): at android.view.View.performClick(View.java:2408)
06-09 05:38:23.774: ERROR/AndroidRuntime(551): at android.view.View$PerformClick.run(View.java:8816)
06-09 05:38:23.774: ERROR/AndroidRuntime(551): at android.os.Handler.handleCallback(Handler.java:587)
06-09 05:38:23.774: ERROR/AndroidRuntime(551): at android.os.Handler.dispatchMessage(Handler.java:92)
06-09 05:38:23.774: ERROR/AndroidRuntime(551): at android.os.Looper.loop(Looper.java:123)
06-09 05:38:23.774: ERROR/AndroidRuntime(551): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-09 05:38:23.774: ERROR/AndroidRuntime(551): at java.lang.reflect.Method.invokeNative(Native Method)
06-09 05:38:23.774: ERROR/AndroidRuntime(551): at java.lang.reflect.Method.invoke(Method.java:521)
06-09 05:38:23.774: ERROR/AndroidRuntime(551): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-09 05:38:23.774: ERROR/AndroidRuntime(551): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-09 05:38:23.774: ERROR/AndroidRuntime(551): at dalvik.system.NativeStart.main(Native Method)
06-09 05:38:23.794: WARN/ActivityManager(70): Force finishing activity com.bfarago.clickme/.Start
06-09 05:38:24.357: WARN/ActivityManager(70): Activity pause timeout for HistoryRecord{43efc7b0 com.bfarago.clickme/.Start}
Line 113 is the mp.start();
I can't see the rest of your code but in your code you have mp.release() called when MediaPlayer mp finished playing and after that the object mp is no longer available so you need to create it again.
精彩评论