2016-04-25 13 views
2

私のプログラムでsoundpoolクラスを使用することに問題があります。私はサウンドプールビルダを開始し、問題なくサウンドをロードしました。特定のサウンドをオン/オフする複数のトグルボタンを使用できるようにしたい。トグルボタンのオン/オフを切り替えることができますが、たとえば、2つ以上のトグルボタンをオンにして1つを一時停止すると、残りの部分が停止します。私はそれを望んでいない、私は他の音が奏で続けることを望む。ここに私のコードです。また、誰かが私がそれを助けることができるビットをきれいにすることができます。前もって感謝します。Android、Soundpoolクラス - ループ中に自動終了の問題が発生する

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     bT1 = (ToggleButton) findViewById(R.id.firstToggle); 
     bT2 = (ToggleButton) findViewById(R.id.secondToggle); 
     bT1.setOnClickListener(this); 
     bT2.setOnClickListener(this); 
     bT3 = (ToggleButton) findViewById(R.id.thirdToggle); 
     bT4 = (ToggleButton) findViewById(R.id.fourthToggle); 
     bT3.setOnClickListener(this); 
     bT4.setOnClickListener(this); 
     bT5 = (ToggleButton) findViewById(R.id.fifthToggle); 
     bT6 = (ToggleButton) findViewById(R.id.sixToggle); 
     bT5.setOnClickListener(this); 
     bT6.setOnClickListener(this); 
     bT7 = (ToggleButton) findViewById(R.id.seventhToggle); 
     bT8 = (ToggleButton) findViewById(R.id.eighthToggle); 
     bT7.setOnClickListener(this); 
     bT8.setOnClickListener(this); 

     initionalizeSoundpool(); 
    } 

    private void initionalizeSoundpool() { 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      AudioAttributes audioAttributes = new AudioAttributes.Builder() 
        .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) 
        .setUsage(AudioAttributes.USAGE_MEDIA) 
        .build(); 

      ourSounds = new SoundPool.Builder() 
        .setMaxStreams(8) 
        .setAudioAttributes(audioAttributes) 
        .build(); 
      soundFx = ourSounds.load(this, R.raw.highhatandrims, 1); 
      beatOne = ourSounds.load(this, R.raw.technodrums, 1); 
      soundFx2 = ourSounds.load(this, R.raw.soothing, 1); 
      beatTwo = ourSounds.load(this, R.raw.thumpohyeahbeat, 1); 
      soundFx3 = ourSounds.load(this, R.raw.dreamyone, 1); 
      beatThree = ourSounds.load(this, R.raw.atredundantsynthesis, 1); 
      soundFx4 = ourSounds.load(this, R.raw.tawaka, 1); 
      clap = ourSounds.load(this, R.raw.cabessa, 1); 
     } else { 
      ourSounds = new SoundPool(4, AudioManager.STREAM_MUSIC,1); 
      soundFx = ourSounds.load(this, R.raw.highhatandrims, 1); 
      beatOne = ourSounds.load(this, R.raw.technodrums, 1); 
      soundFx2 = ourSounds.load(this, R.raw.soothing, 1); 
      beatTwo = ourSounds.load(this, R.raw.thumpohyeahbeat, 1); 
      soundFx3 = ourSounds.load(this, R.raw.dreamyone, 1); 
      beatThree = ourSounds.load(this, R.raw.atredundantsynthesis, 1); 
      soundFx4 = ourSounds.load(this, R.raw.tawaka, 1); 
      clap = ourSounds.load(this, R.raw.cabessa, 1); 
     } 

    } 




public void onClick(View view) { 
     switch (view.getId()) { 
      case R.id.firstToggle: 
       if(bT1.isChecked()) { 
        ourSounds.play(soundFx, 1, 1, 1, -1, 1); 
       } 
       else { 
        ourSounds.autoPause(); 
       } 
       break; 
      case R.id.secondToggle: 
       if(bT2.isChecked()) { 
        ourSounds.play(beatOne, 1, 1, 1, -1, 1); 
       } 
       else { 
        ourSounds.autoPause(); 
       } 
       break; 
      case R.id.thirdToggle: 
       if(bT3.isChecked()) { 
        ourSounds.play(soundFx2, 1, 1, 1, -1, 1); 
       } 
       else { 
        ourSounds.autoPause(); 
       } 
       break; 
      case R.id.fourthToggle: 
       if(bT4.isChecked()) { 
        ourSounds.play(beatTwo, 1, 1, 1, -1, 1); 
       } 
       else { 
        ourSounds.autoPause(); 
       } 
       break; 
+0

長さが何でありますかサウンドファイルの? –

+0

短いサンプルで、おそらく5〜10秒です。 – wth7777

答えて

1

あなたOnPause方法にこれを追加します。

@Override 
public void onPause() { 
    super.onPause(); 

    ourSounds.release(); 
    ourSounds = null; 
} 

更新:

代わりのourSounds.autoPause();例えばourSounds.pause(soundID);を使用してみてください。

case R.id.firstToggle: 
      if(bT1.isChecked()) { 
       ourSounds.play(soundFx, 1, 1, 1, -1, 1); 
      } 
      else { 
       ourSounds.pause(soundFx); 
      } 
      break; 
... 
+0

私は、これらの2行のコードを自動終了のすぐ下のelse文に入れようとしましたが、それは機能しませんでした。私は2つまたは3つのボタンを押してすべてを再生しますが、1つを一時停止すると他のボタンが止まり、プログラムから私を追い出すことになります。しかし、提案をありがとう。 – wth7777

+0

いいえ、私は 'OnPause'メソッドの中で、更新された答えを確認してください。 –

+0

まだ初心者なので、私は理解に問題があります。私はonPauseメソッドまたはonResumeを持っていません。 onPauseを作成すると、onResumeが必要ですか?どこで私のelse文でonPauseを呼び出すのですか? onPauseにintを渡す必要がありますか?あなたの助けをもう一度ありがとう。 – wth7777

関連する問題