我试图每次播放声音,都单击一个按钮。声音会播放,但是我需要每次播放一次(就像键入内容时播放的声音一样),问题是我的声音并不总是播放。我认为这是因为单击按钮的延迟很小。
new MediaPlayer sound = new MediaPlayer().create(getApplicationContext(), R.raw.sound);
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (x >= pS.getX() && x < (pS.getX() + squareS.getWidth()) &&
y >= pS.getY() && y < (pS.getY() + squareS.getHeight())) {
pS.setRes(pSquareOffRes);
pS.setX(pS.getX() - squareS.getWidth());
pS.setY(pS.getY() - squareS.getHeight());
pS.setTouched(true);
pS.lock(true);
touchLimitA = System.currentTimeMillis();
timer = 3;
sound.start();
break;
}
return false;
}
我很高兴您可以提供任何建议:)。
最佳答案
如果您需要低延迟播放,则应使用SoundPool
关于java - 单击按钮时如何快速播放声音(android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31411033/