當我單擊應用程序中的按鈕時,它會完美播放聲音。但是,觸發啓動和停止的按鈕以及手機的後退按鈕可以再次點擊,但聲音仍在播放。在橢圓和手機中都沒有顯示錯誤信息。我正在製作一個android節拍器應用程序。但是,播放聲音時程序停止
我知道問題是我的程序被困在for循環播放聲音。 但是,我不知道如何使它無需使用無限循環,如果我想循環它無限。任何建議來解決這個問題?
這個節拍器的聲音就像嘀嗒,嘀嗒,嘀嗒,敲擊...... 我想用按鈕來觸發聲音。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
final Button button = (Button) findViewById(R.id.startStop);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
boolean start = false;
if (start == false) {
play();
} else if (start == true) {
start = false;
}
}
});
}
public void play() {
final SoundPool sndPool1 = new SoundPool(3, AudioManager.STREAM_MUSIC,
0);
final SoundPool sndPool2 = new SoundPool(3, AudioManager.STREAM_MUSIC,
0);
final int ticks = sndPool1.load(this, R.raw.tick, 1);
final int tocks = sndPool2.load(this, R.raw.tock, 1);
for (;;) {
sndPool2.play(tocks, 1f, 1f, 1, 0, 1f);
LockSupport.parkNanos(((240000/200)/4) * 1000000);
sndPool2.play(tocks, 1f, 1f, 1, 0, 1f);
LockSupport.parkNanos(((240000/200)/4) * 1000000);
sndPool2.play(tocks, 1f, 1f, 1, 0, 1f);
LockSupport.parkNanos(((240000/200)/4) * 1000000);
sndPool1.play(ticks, 1f, 1f, 1, 0, 1f);
LockSupport.parkNanos(((240000/200)/4) * 1000000);
}
}
你有沒有爲我們logcat的? –