0

我正在尝试播放随机声音 onTouch 事件(当前正在工作),但声音实际上播放了两次 onTouch 事件,而不是按预期播放一次。我假设这是由于 UP 和 Down onTouch 事件。有什么想法吗?

注意:我在测试时注释掉了额外的随机声音文件。

package com.tmapps.wiub;

import java.util.Random;
import com.tmapps.wiub.SoundManager;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

public class Soundboard extends Activity {
    private SoundManager mSoundManager;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // FULLSCREEN CODE
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);

        mSoundManager = new SoundManager();
        mSoundManager.initSounds(getBaseContext());
        mSoundManager.addSound(0, R.raw.sound0);

     // COMMENTED OUT WHILE TESTING  
     // mSoundManager.addSound(1, R.raw.sound1);
     // mSoundManager.addSound(2, R.raw.sound2);
     // mSoundManager.addSound(3, R.raw.sound3);
     // mSoundManager.addSound(4, R.raw.sound4);
     // mSoundManager.addSound(5, R.raw.sound5);
     // mSoundManager.addSound(6, R.raw.sound6);
     // mSoundManager.addSound(7, R.raw.sound7);
     // mSoundManager.addSound(8, R.raw.sound8);
     // mSoundManager.addSound(9, R.raw.sound9);

    };

        Random r = new Random();
        int x = r.nextInt(1);

        switch (evt.getAction()) 
        {
            case MotionEvent.ACTION_DOWN:
            mSoundManager.playSound(x);
            return super.onTouchEvent(evt);

            case MotionEvent.ACTION_UP:
            break;
            default:
            break;
        }
4

1 回答 1

0

那是因为 ACTION_DOWN 和 ACTION_UP 都在触发,只在其中一个上实现代码

    public boolean onTouchEvent(MotionEvent evt) 
    {
        switch (evt.getAction()) 
        {
            case MotionEvent.ACTION_DOWN: 
            break;
            case MotionEvent.ACTION_UP:
            break;
            default:
            break;
        }
        return true;
    }
于 2012-04-11T23:13:36.523 回答