我得到的建议是,我应该使用Soundpool而不是Mediaplayer来点击按钮点击很多短音,但我真的不明白如何让它工作。这是我的代码:
protected void onCreate(Bundle buttons) {
super.onCreate(buttons);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
//SoundPool initialization somewhere
SoundPool pool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
//Load your sound effect into the pool
int soundId = pool.load(this, R.raw.SOUND, 1); (How to load more sounds?)
List<Integer> streams = new ArrayList<Integer>();
Button PLAY= (Button)findViewById(R.id.PLAY);
PLAY.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int streamId = pool.play(SOUND, 1.0f, 1.0f, 1, 0, 1.0f);
streams.add(streamId);
}
});
Button stop = (Button)findViewById(R.id.stop);
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
for (Integer stream : streams) {
pool.stop(stream);
}
streams.clear();
}
});
}