播放随机声音onTouch

时间:2012-04-11 23:09:09

标签: java android ontouchevent

我正在尝试播放随机声音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;
        }

1 个答案:

答案 0 :(得分: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;
    }