0

私はまだAndroid開発に不慣れで、行き詰まっています。回転するグラフィックを強化し、短いサウンドエフェクト(ビープ音)の再生をトリガーする背景int[カウンター]を増やすonTouchEventがあります。現在、[counter]> 10の場合、サウンドは(サウンドプールを使用して)再生され、onTouchEventがcounter> 10を維持している限り、一時的にループされます。ビープ音効果の再生間に遅延を挿入して、遅延を発生させることはできますか? [counter]の値が大きくなるほど減少しますか?

例:if(counter == 10){delay = 80 // in MS} if(counter == 90){delay = 10 // in MS}

私のコード:

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);
    }
}

編集:コードを追加

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);
    }
}
4

1 に答える 1

0

このようなものはどうですか?

  if (counter > 10) {
      sm.playSound(1);  // plays beep 
      INTERVAL = 100 - counter;
      handler.postDelayed(this, INTERVAL); 
于 2012-05-31T21:42:27.327 に答える