我试图将其设置setLooping
为 true,但没有任何结果。我希望在检测到事件时重复声音效果,并在释放时停止重复。
public class MainActivity extends Activity implements SurfaceHolder.Callback {
MediaPlayer player;
public void onCreate(Bundle savedInstanceState) {
player = MediaPlayer.create(this, R.raw.rifle);
player.setLooping(true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
int ey = (int) event.getY();
if (ey > height / 2) {
player.start();
}
}
return super.onTouchEvent(event);
}
}