Android,Soundpool Class - 循环声音时的自动暂停问题

时间:2016-04-25 22:52:56

标签: java android android-studio audio soundpool

我在使用soundpool类的程序中遇到问题。我启动了soundpool构建器并加载了我的声音没有任何问题。我希望能够使用多个打开和关闭特定声音的togglebuttons。我可以打开和关闭togglebuttons,但是例如,如果我打开两个或更多togglebutton而不是我暂停一个.....其余的将停止。我不想那样,我希望其他声音继续播放。这是我的代码。此外,如果有人可以帮我清理一下会有所帮助。提前谢谢。

 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;

1 个答案:

答案 0 :(得分: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; ...

consumer.init().then(function() {
  console.log("Consumer Ready");
});
app.get('/:topic/:off', function(req, res) {
console.log(req.params.off);
consumer.subscribe(req.params.topic, 0, {
  offset: req.params.off
},
function(messageSet, topic, partition) {
  var msg = "";
  messageSet.some(function(m) {
    msg += m.message.value.toString('utf8') + " ";
    if (parseInt(m.offset, 10) > parseInt(req.params.off, 10) + 10) {
      res.send("Results: " + req.params.off + "  " + msg);
      return true;
    }
  });
});
});