2014-01-07 23 views
1

當我單擊應用程序中的按鈕時,它會完美播放聲音。但是,觸發啓動和停止的按鈕以及手機的後退按鈕可以再次點擊,但聲音仍在播放。在橢圓和手機中都沒有顯示錯誤信息。我正在製作一個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); 
    } 
} 
+0

你有沒有爲我們logcat的? –

回答

2

你的問題是你正在UI線程上運行一個無限循環。這應該在後臺線程中完成。下面是一個使用AsyncTask的例子。

public class BackgroundSound extends AsyncTask<Void, Void, Void> { 

private Context context; 

public BackgroundSound(Context context) { 
    this.context = context; 
} 

@Override 
protected Void doInBackground(Void... params) { 

    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(context, R.raw.tick, 1); 
    final int tocks = sndPool2.load(context, 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); 
    } 
} 

} 

,並致電BackgroundSound任務是這樣的:

public class MainActivity extends Activity { 

private Activity activity; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    activity = this; 
    setVolumeControlStream(AudioManager.STREAM_MUSIC); 
    final Button button = (Button) findViewById(R.id.startStop); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      new BackgroundSound(activity).execute(); 
     } 
    }); 

} 
} 
+0

在「final int ticks = sndPool1.load(this,R.raw.tick,1);」 日食下劃線「負載」。 當我將其更改爲getBaseContext時,它強調了「getBaseContext()」 應該在裏面放什麼? –

+0

對不起。在你的onClickListener中,'this'不再引用Activity,而是引用onClickListener。如果你的AsyncTask在你的Activity中,那麼你可以設置一個類變量activity,你可以在onCreate中設置它。然後,將它傳遞給你的'load()'函數。 –

+0

對不起,我不知道你設置一個類變量的意思。 –

相關問題