I’m still new to Android dev and am stuck. I have an onTouchEvent that powers a rotating graphic and increases a background int [counter] which triggers a short sound effect (beep) to be played. Currently, if [counter] > 10 a the sound is played (using the soundpool) and is summarily looped as long as the onTouchEvent keeps counter > 10. Is it possible to insert a delay between playbacks of the beep sound effect so that the delay decreases the larger the value of [counter] gets?
Example:
if (counter == 10) { delay = 80 // in MS }
if (counter == 90) { delay = 10 // in MS }
My Code:
sm = new SoundManager();
sm.initSounds(getBaseContext());
sm.addSound(1, R.raw.beep);
...
@Override
public boolean onTouchEvent(MotionEvent event) {
if (buttonClicked) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
counter++;
startRotating();
break;
case MotionEvent.ACTION_UP:
counter--;
stopRotating();
break;
}
}
return super.onTouchEvent(event);
}
public void startRotating() {
returnRotating = false;
if (!keepRotating) {
keepRotating = true;
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (keepRotating) {
degrees = (degrees + 10) % 360;
make(degrees);
counter = counter + 1;
if (counter > 10)
sm.playSound(1); // plays beep
handler.postDelayed(this, INTERVAL);
}
}
}, INTERVAL);
}
}
EDIT: Added code
public void stopRotating() {
keepRotating = false;
if (!returnRotating) {
returnRotating = true;
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (returnRotating) {
degrees = (degrees - 10) % 360;
make(degrees);
counter = counter - 1;
if (counter > 10) {
sm.playSound(1); // plays beep
handler.postDelayed(this, INTERVAL);
}
}
}, INTERVAL);
}
}
How about something like this?