Seekbar控制Soundpool,停止工作

时间:2017-04-27 14:20:53

标签: android volume soundpool

所以我有8张图片,每张图片都会开始循环播放8种不同的声音。我在每张图像下都有一个搜索栏来控制该声音的音量。

搜索栏在第一次声音开始时工作正常,但是当停止并再次启动时,搜索栏不再控制音量。我究竟做错了什么? `noiseVolumeControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            double noiseVol = (double) progress / (double) maxVolume;

            Log.i("Noise Volume", String.valueOf(progress) + " " + String.valueOf((float) noiseVol));
            mySound.setVolume(whiteNoiseId, (float) noiseVol, (float) noiseVol);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

public void whiteNoiseTapped(View view){

    Log.i("White Noise", "button tapped");

    if (whiteNoisePlaying) {
        Log.i("White Noise", "already playing");
        mySound.stop(noiseStreaming);
        whiteNoisePlaying = false;
        ((ImageView) view).setImageResource(R.drawable.whitenoise);
        isPlaying--;
    } else if (isPlaying < 3) {
        Log.i("White Noise", "start playing");
        noiseStreaming = mySound.play(whiteNoiseId, (float) noiseVol, (float) noiseVol, 1, -1, 1);
        whiteNoisePlaying = true;
        ((ImageView) view).setImageResource(R.drawable.whitenoisepressed);
        isPlaying++;
    }
}`

1 个答案:

答案 0 :(得分:0)

我明白了。我需要一种方法来了解我之前是否已经按过按钮。我最初将noiseStreaming设置为0,然后在按下按钮时检查它。如果它为0,那么我将whiteNoiseId设置为noiseStreaming。下次它不会为0并且会跳过该位 -

public void whiteNoiseTapped(View view){

    Log.i("White Noise", "button tapped");

    if (whiteNoisePlaying) {
        Log.i("White Noise", "already playing");
        mySound.pause(noiseStreaming);
        whiteNoisePlaying = false;
        ((ImageView) view).setImageResource(R.drawable.whitenoise);
        isPlaying--;
    } else if (isPlaying < 3) {
        Log.i("White Noise", "start playing");
        if (noiseStreaming == 0) {
            noiseStreaming = mySound.play(whiteNoiseId, (float) noiseVol, (float) noiseVol, 1, -1, 1);
        } else {
            mySound.resume(noiseStreaming);
        }
        whiteNoisePlaying = true;
        ((ImageView) view).setImageResource(R.drawable.whitenoisepressed);
        isPlaying++;
    }
}

现在可以通过点击开启和关闭来控制音量。