我写了一个应用程序,当我按下按钮时应该播放声音。但是声音大约每四次点击播放一次,而不是完整的长度。
@Override
public void onClick(View v) {
if (v == bplus) {
vib.vibrate(100);
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.beep);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
counter++;
}
当我在 mp.start() 之前使用 mp.prepare() 时,声音仍然不规则,我得到了这个:
02-16 23:19:41.761: E/MediaPlayer(750): prepareAsync called in state 8
02-16 23:19:41.761: W/System.err(750): java.lang.IllegalStateException
02-16 23:19:41.771: W/System.err(750): at android.media.MediaPlayer.prepare(Native Method)
02-16 23:19:41.781: W/System.err(750): at de.christianschmitz.counter.Starter.onClick(Starter.java:100)
02-16 23:19:41.791: W/System.err(750): at android.view.View.performClick(View.java:3480)
02-16 23:19:41.791: W/System.err(750): at android.view.View$PerformClick.run(View.java:13983)
02-16 23:19:41.801: W/System.err(750): at android.os.Handler.handleCallback(Handler.java:605)
02-16 23:19:41.801: W/System.err(750): at android.os.Handler.dispatchMessage(Handler.java:92)
02-16 23:19:41.811: W/System.err(750): at android.os.Looper.loop(Looper.java:137)
02-16 23:19:41.811: W/System.err(750): at android.app.ActivityThread.main(ActivityThread.java:4340)
02-16 23:19:41.821: W/System.err(750): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 23:19:41.831: W/System.err(750): at java.lang.reflect.Method.invoke(Method.java:511)
02-16 23:19:41.831: W/System.err(750): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-16 23:19:41.841: W/System.err(750): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-16 23:19:41.851: W/System.err(750): at dalvik.system.NativeStart.main(Native Method)
02-16 23:19:41.851: E/MediaPlayer(750): error (-19, 0)