我正在制作一个钢琴应用程序,其中有九个按钮,当我们触摸按钮时,每个按钮播放不同的声音。但现在问题是 -
1)当我们触摸按钮时,它只播放该按钮的声音,并且在移动手指时没有播放下一个按钮的声音。
2)如果我使用ACTION_UP来停止声音,那么当我们从按钮上抬起手指时,它会停止声音,而不会完成该按钮的完整声音。
public class Piano_view extends Activity implements View.OnTouchListener {
Button sound1, sound2, sound3, sound4, sound5, sound6, sound7, sound8,
sound9;
MediaPlayer mp1, mp2, mp3, mp4, mp5, mp6, mp7, mp8, mp9;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.piano_view);
initMpFiles();
setVolumeControlStream(AudioManager.STREAM_MUSIC);
sound1 = (Button) findViewById(R.id.one);
sound2 = (Button) findViewById(R.id.second);
sound3 = (Button) findViewById(R.id.three);
sound4 = (Button) findViewById(R.id.four);
sound5 = (Button) findViewById(R.id.five);
sound6 = (Button) findViewById(R.id.six);
sound7 = (Button) findViewById(R.id.seven);
sound8 = (Button) findViewById(R.id.eight);
sound9 = (Button) findViewById(R.id.nine);
sound1.setOnTouchListener(this);
sound2.setOnTouchListener(this);
sound3.setOnTouchListener(this);
sound4.setOnTouchListener(this);
sound5.setOnTouchListener(this);
sound6.setOnTouchListener(this);
sound7.setOnTouchListener(this);
sound8.setOnTouchListener(this);
sound9.setOnTouchListener(this);
}
private void initMpFiles() {
mp1 = MediaPlayer.create(Piano_view.this, R.raw.a4);
mp2 = MediaPlayer.create(Piano_view.this, R.raw.b4);
mp3 = MediaPlayer.create(Piano_view.this, R.raw.c4);
mp4 = MediaPlayer.create(Piano_view.this, R.raw.c5);
mp5 = MediaPlayer.create(Piano_view.this, R.raw.e4);
mp6 = MediaPlayer.create(Piano_view.this, R.raw.b4);
mp7 = MediaPlayer.create(Piano_view.this, R.raw.f4);
mp8 = MediaPlayer.create(Piano_view.this, R.raw.d4);
mp9 = MediaPlayer.create(Piano_view.this, R.raw.g4);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int id = v.getId();
switch (id) {
case R.id.one:
float x = event.getX();
float y = event.getY();
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
mp1.start();
mp1.seekTo(0);
v.setPressed(true);
return true;
}
break;
case R.id.second:
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
mp2.start();
mp2.seekTo(0);
v.setPressed(true);
return true;
}
break;
case R.id.three:
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
mp3.start();
mp3.seekTo(0);
v.setPressed(true);
return true;
}
break;
case R.id.four:
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
mp4.start();
mp4.seekTo(0);
v.setPressed(true);
return true;
}
break;
case R.id.five:
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
mp5.start();
mp5.seekTo(0);
v.setPressed(true);
return true;
}
break;
case R.id.six:
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
mp6.start();
mp6.seekTo(0);
v.setPressed(true);
return true;
}
break;
case R.id.seven:
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
mp7.start();
mp7.seekTo(0);
v.setPressed(true);
return true;
}
break;
case R.id.eight:
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
mp8.start();
mp8.seekTo(0);
v.setPressed(true);
return true;
}
break;
case R.id.nine:
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
mp9.start();
mp9.seekTo(0);
v.setPressed(true);
return true;
}
break;
}
return true;
}
}
答案 0 :(得分:0)
在这种情况下,你不应该使用Button.Draw包含" 9按钮" 的视图,并由你自己处理触摸事件。
答案 1 :(得分:0)
您可以使用onTouch并在新主题中启动音乐。如果你正在移动线程继续运行。 您可以使用处理程序。
Handler handler = new Handler();
在你的onTouch监听器中。如果它与ACTION_DOWN一起使用,我不会感到害羞。您还可以添加ACTION_MOVE以检测移动。
int x = (int) motionEvent.getRawX() - coords[0];
int y = (int) motionEvent.getRawY() - coords[1];
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN || event.getAction() == android.view.MotionEvent.ACTION_MOVE) {
if(put your test with the coodinates here){
handler.post(runMp1);
return true;
}
}
break;
现在你需要检查触摸走廊是否在按钮中。一个例子可能是
int M1right = (int) button.getRight();
int M1top = (int) button.getTop();
int M1left = (int) button.getLeft();
int M1bottom = (int) button.getBottom();
if(x < M1right && x > M1left && y < M1bottom && y > M1top)
然后你需要一个线程
private Runnable runMp1 = new Runnable() {
@Override
public void run() {
mp1.start();
mp1.seekTo(0);
v.setPressed(true);
}
};
另外你可以添加一个bool var,它被设置为fase直到声音结束。只有当此var为true时,处理程序才会启动。
希望能解决您的问题。祝你有愉快的一天!