0

我有两个按钮,如果按下它们就会发出声音。我想同时按下这两个键来同时听到这两种声音。至今:

@Override // 处理导航箭头的触摸 public boolean onTouch(View v, MotionEvent event) {

    int action = event.getActionMasked();

    switch (action){

    case MotionEvent.ACTION_DOWN:
         switch (v.getId()) { // Which button?
            case R.id.cnotebutton: // The upArrow
                Log.i("C", "C");
              //  mSoundPool.stop(mStream1);
                //mStream1= mSoundPool.play(mSoundPoolMap.get(1), streamVolume, streamVolume, 1,0, 1f);
                break;
            case R.id.dnotebutton: // The downArrow
                  Log.i("D", "D");
                 // mSoundPool.stop(mStream2);
                //mStream2= mSoundPool.play(mSoundPoolMap.get(2), streamVolume, streamVolume, 1,0, 1f);
                break;
            default:
            }

            break;
        case MotionEvent.ACTION_UP:

            break;  
        case MotionEvent.ACTION_POINTER_DOWN:
             switch (v.getId()) { // Which button?
                case R.id.cnotebutton: // The upArrow
                    Log.i("C", "C");
                  //  mSoundPool.stop(mStream1);
                    //mStream1= mSoundPool.play(mSoundPoolMap.get(1), streamVolume, streamVolume, 1,0, 1f);
                    break;
                case R.id.dnotebutton: // The downArrow
                      Log.i("D", "D");
                     // mSoundPool.stop(mStream2);
                    //mStream2= mSoundPool.play(mSoundPoolMap.get(2), streamVolume, streamVolume, 1,0, 1f);
                    break;
                default:
                }
            break;
        case MotionEvent.ACTION_POINTER_UP:

            break;
        case MotionEvent.ACTION_MOVE:

            break;
        default:

    }
    return true;
}

这没用。我应该如何解决这个问题?另一个问题,如果我在屏幕上移动手指,我想在手指到达按钮时按下按钮而不抬起它。ACTION_MOVE 也不起作用。抱歉这个愚蠢的问题我在这个领域很初学者。

更新

使用此代码,它可以检测到多点触控,但我总是得到两个相同的日志,就好像我用两根手指总是触摸同一个按钮一样。因此,即使我同时按下两个按钮,我得到一个日志“C”和另一个错误的“C”,我应该得到 C 和 D。

感谢提前!

4

2 回答 2

0

你的代码看起来像这样吗?我强调了一些关键点

包be.domore.smokesignaltest;

导入android.app.Activity;导入android.os.Bundle;导入 android.view.MotionEvent;导入android.view.View;导入 android.view.View.OnTouchListener;导入android.widget.Button;

公共类 DummyActivity 扩展 Activity 实现 OnTouchListener { private Button myButton1; 私人按钮 myButton2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set the content view
    setContentView(R.layout.activity_dummy);

    // Map controls
    mapControls();
}

private void mapControls() {
    myButton1 = (Button) findViewById(R.id.myButton1);
    myButton1.setOnTouchListener(this);

    myButton2 = (Button) findViewById(R.id.myButton2);
    myButton2.setOnTouchListener(this);
}

public boolean onTouch(View v, MotionEvent event) {
    **if (event.getAction() == MotionEvent.ACTION_DOWN)**
        switch (v.getId()) {
        case R.id.myButton1:
            // TODO: put your code here
            **return true;**
        case R.id.myButton2:
            // TODO: put your code here
            **return true;**
        }

    *return false;*
}

}

以下类是我创建的用于管理声音的助手。有趣的部分是函数 AssignSoundToView。不知道对你有没有帮助?

包be.domore.FingerTwister.Game;

导入 java.io.IOException;

导入 be.domore.FingerTwister.Settings;

导入android.app.Activity;导入 android.content.res.AssetFileDescriptor;导入 android.content.res.AssetManager;导入android.media.AudioManager;导入android.media.MediaPlayer;导入 android.media.SoundPool;导入android.util.Log;导入 android.view.MotionEvent;导入android.view.View;导入 android.view.View.OnTouchListener;

公共类 SoundManager2 { 私有静态 SoundManager2 当前 = 新 SoundManager2();

public static SoundManager2 getCurrent() {
    return current;
}

private boolean initialized = false;
private MediaPlayer player;
private SoundPool pool;
private AssetManager assetManager;

private int[] soundId = new int[Sounds.values().length];
private int[] soundStream = new int[Sounds.values().length];

private SoundManager2() {

}

public void assignSoundToView(View view, Sounds sound) {
    final int currentSound;

    if (Settings.hasFx() == false) {

    } else if (sound != Sounds.none) {
        currentSound = sound.ordinal();

        view.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                try {
                    if (event.getAction() == MotionEvent.ACTION_DOWN)
                        pool.play(soundId[currentSound], 1.0f, 1.0f, 0, 0,
                                1);
                } finally {
                }
                return false;
            }
        });
    }
}

public boolean initialize(Activity activity) {

    if (initialized == false) {
        player = new MediaPlayer();
        pool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
        assetManager = activity.getAssets();
        activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);

        for (int idx = 1; idx < Sounds.values().length; idx++)
            soundId[idx] = loadSound(
                    String.format("%s.mp3", Sounds.values()[idx]),
                    assetManager);

        initialized = true;
    } else
        Log.w("SoundManager", "SoundManager was already initialized");

    return initialized;
}

public boolean isInitialized() {
    return initialized;
}

private int loadSound(String filename, AssetManager assetManager) {
    int rc;
    AssetFileDescriptor afd;

    try {
        afd = assetManager.openFd(filename);
        rc = pool.load(afd, 1);
    } catch (IOException e) {
        e.printStackTrace();
        rc = 0;
    }

    return rc;
}

public boolean playMusic(String filename) {
    boolean rc = false;
    AssetFileDescriptor descriptor;

    if (initialized == false)
        Log.e("SoundManager", "SoundManager is not initialized");
    else
        try {
            descriptor = assetManager.openFd(filename);
            player.reset();
            player.setDataSource(descriptor.getFileDescriptor(),
                    descriptor.getStartOffset(),
                    descriptor.getDeclaredLength());
            player.setLooping(true);
            // player.setVolume(1.0f, 1.0f);
            player.prepare();
            player.start();

            rc = true;
        } catch (Exception e) {
            e.printStackTrace();
        }

    return rc;
}

public void playSound(Sounds sound) {
    int index;

    index = sound.ordinal();
    soundStream[index] = pool.play(soundId[index], 1.0f, 1.0f, 0, 0, 1);
}

public void stopAllSound() {
    for (int idx = 1; idx < Sounds.values().length; idx++)
        stopSound(Sounds.values()[idx]);
}

public boolean stopMusic() {
    boolean rc = false;

    if (initialized == false)
        Log.e("SoundManager", "SoundManager is not initialized");
    else {
        player.stop();
        rc = true;
    }

    return rc;
}

public void stopSound(Sounds sound) {
    int index;

    index = sound.ordinal();
    pool.stop(soundStream[index]);
}

}

于 2012-12-04T23:07:14.173 回答
0

你在玩 SurfaceView 吗?我感觉不是。为什么不简单设置每个Button的OnClickListener呢?

否则,您需要分别访问每个触摸事件:请参阅http://android-developers.blogspot.be/2010/06/making-sense-of-multitouch.html上的“索引与 ID”段落:也许会有所帮助你

哔叽

于 2012-12-04T21:46:09.470 回答