当两个按钮同时触摸时,我试图一起播放两个声音。我有button1和button2,而button1是触摸,button2不会播放声音。但当我把手指从按钮1上移开然后触摸按钮2播放声音。
我试过谷歌搜索,但无法找到答案。 我想做的很清楚。当两个按钮碰在一起时,一起播放两个声音。
到目前为止,我的代码如下。
--dry-run
}
public class MultitouchtestActivity extends MultiTouchActivity {
/** Called when the activity is first created. */
private Button btn1;
private Button btn2;
SoundPool soundPool;
AudioManager audioManager;
int soundId;
int kID, sID;
@Override
public void onCreate(Bundle savedInstanceState)
{
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
super.onCreate(savedInstanceState);
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
//the maximum number of simultaneous streams for this SoundPool object
int maxStreams = 4;
//the audio stream type as described in AudioManager
int streamType = AudioManager.STREAM_MUSIC;
//the sample-rate converter quality. Currently has no effect. Use 0 for the default.
int srcQuality = 0;
soundPool = new SoundPool(maxStreams, streamType, srcQuality);
soundPool.setOnLoadCompleteListener(soundPoolOnLoadCompleteListener);
soundId = soundPool.load(this, R.raw.c, 1);
kID = soundPool.load(this, R.raw.k, 1);
sID = soundPool.load(this, R.raw.s, 1);
btn1.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
float vol = audioManager.getStreamVolume(
AudioManager.STREAM_MUSIC);
float maxVol = audioManager.getStreamMaxVolume(
AudioManager.STREAM_MUSIC);
float leftVolume = vol/maxVol;
float rightVolume = vol/maxVol;
int priority = 1;
int no_loop = 0;
float normal_playback_rate = 1f;
soundPool.play(kID,
leftVolume,
rightVolume,
priority,
no_loop,
normal_playback_rate);
return true;
}
return false;
}
});
btn2.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
float vol = audioManager.getStreamVolume(
AudioManager.STREAM_MUSIC);
float maxVol = audioManager.getStreamMaxVolume(
AudioManager.STREAM_MUSIC);
float leftVolume = vol/maxVol;
float rightVolume = vol/maxVol;
int priority = 1;
int no_loop = 0;
float normal_playback_rate = 1f;
soundPool.play(sID,
leftVolume,
rightVolume,
priority,
no_loop,
normal_playback_rate);
return true;
}
return false;
}
});
}
SoundPool.OnLoadCompleteListener soundPoolOnLoadCompleteListener = new SoundPool.OnLoadCompleteListener()
{
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status)
{
if(status==0)
{
btn1.setEnabled(true);
btn2.setEnabled(true);
}
else
{
Toast.makeText(MultitouchtestActivity.this,"SoundPool.load() fail", Toast.LENGTH_LONG).show();
}
}
};
}