这是我的Activity
:
public class MainActivity extends AppCompatActivity {
private SoundPool soundPool;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
final int sound1 = soundPool.load(this, R.raw.whack, 1);
final int sound2 = soundPool.load(this, R.raw.miss, 1);
Button b1 = (Button) findViewById(R.id.b1);
Button b2 = (Button) findViewById(R.id.b2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
playSound(sound1);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
playSound(sound2);
}
});
}
private void playSound(int soundId) {
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
float volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
soundPool.play(soundId, volume, volume, 1, 0, 1);
}
}
问题是当我单击 Button1 时,会播放 sound1。然后,当我一次又一次地单击 Button1 时,在单击 Button2 之前什么都不会播放。当我这样做时,会播放 sound2。然后,如果我单击 Button1,则会播放 sound1。当我一遍又一遍地单击 Button2 时,也会发生同样的情况。那么这里的问题是什么?