我有一个持续7秒的声音。我想在按下时播放它,在按下时停止它(例如1秒)。我的代码如下所示,但我有一个问题?当快速按压几次时,声音播放7秒,不要停止。我想做一架简单的钢琴。有什么问题吗?你对此有什么想法吗?
c.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: // Button Pressed
SID1_c = soundPool.play(sound_c, 1, 1, 1, 0, 0);
c.setBackgroundResource(R.drawable.key4);
return true;
case MotionEvent.ACTION_UP:// Button released
handler =new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
soundPool.stop(SID1_c);
}
},90);
return false;
}
return false;
}
});发布于 2018-03-14 07:41:15
首先,在按下button时,需要停止soundPool (如果有的话)。
在再次启动soundPool.stop(SID1_c);之前,用case MotionEvent.ACTION_DOWN:编写soundPool。
https://stackoverflow.com/questions/49271766
复制相似问题