2

如何在 soundpool 中进行多次触摸?我有 3 个按钮。当手指滑动触摸按钮时,就像钢琴中的声音?这是我的代码:

sp = new SoundPool (5, AudioManager.STREAM_MUSIC, 0);
player1 = sp.load(this, R.raw.ndang, 1);
player2 = sp.load(this, R.raw.nding, 1);
player3 = sp.load(this, R.raw.ndung, 1);

ndang = (ImageButton) this.findViewById(R.id.ndang);
ndang.setOnClickListener(new OnClickListener() {
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        sp.play(player1, 1, 1, 0, 0, 1);
    }
});

ndang = (ImageButton) this.findViewById(R.id.ndang2);
ndang.setOnClickListener(new OnClickListener() {
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        sp.play(player1, 1, 1, 0, 0, 1);
    }
});

nding = (ImageButton) this.findViewById(R.id.nding);
nding.setOnClickListener(new OnClickListener() {
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        sp.play(player2, 1, 1, 0, 0, 1);
    }
});
4

2 回答 2

2

愿这对您有所帮助:http: //www.martinhoeller.net/2012/01/13/developing-a-musical-instrument-app-for-android/

于 2012-08-09T09:46:36.470 回答
1

您需要View.OnTouchListener为钢琴实现一个或更好的是创建一个包含整个键盘和覆盖的自定义视图onTouchEvent。这将使您能够在最小化代码的同时检测多个钢琴按键和滑键。

编辑:

public class PianoView extends View {
    @Override
    onTouchEvent(MotionEvent event) {
        //your code to find which key was pressed based on x,y touch
    }
}

或者

public class PianoActivity extends Activity {
    @Override
    dispatchTouchEvent(MotionEvent event) {
        //your code to find which key was pressed based on x,y touch
    }
}
于 2012-08-09T04:14:08.627 回答