以下代码用于处理名为clash的按钮的ACTION_DOWN和ACTION_UP事件。其思想是,一旦if/else确定onTouch事件是由clash和switch语句引起的,就会根据该操作确定要执行的操作。我不知道问题是不是switch语句没有返回true,这可能与问题有关。当我添加一个return时,eclipse说代码是不可访问的,这我不明白。我的印象是,你不能不断线就打破开关。
实际发生的情况是,第一个声音将循环,但代码似乎从未检测到释放按钮时的动作,因此声音永远播放。任何帮助都将不胜感激。
public boolean onTouch(View v, MotionEvent event) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.clash);
if (v.getId() == R.id.clash){
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
mp.setLooping(true);
mp.start();
break;
case MotionEvent.ACTION_UP:
mp.pause();
break;
}
}
return true;
}
});发布于 2010-10-14 04:30:14
//Add the following line in the code before your setOnTouchListener()
MediaPlayer mp;
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.clash){
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mp = MediaPlayer.create(getBaseContext(), R.raw.clash);
mp.setLooping(true);
mp.start();
break;
case MotionEvent.ACTION_UP:
if(mp != null)
{
mp.pause();
mp.release();
}
break;
}
}
}
// I'm assuming this was from the onTouchListener()? -> }); 这只是个想法。
https://stackoverflow.com/questions/3927393
复制相似问题