两个soundpool音量控制

时间:2017-04-27 11:17:50

标签: android volume soundpool

我正在制作一个白噪声应用程序,我想独立控制每个声音的音量。这可能吗?

这是我现在的onCreate。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mySound = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
    whiteNoiseId = mySound.load(this, R.raw.whitenoise, 1);
    rainId = mySound.load(this, R.raw.rain, 1);


    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

    SeekBar noiseVolumeControl = (SeekBar) findViewById(R.id.noiseSeekBar);
    noiseVolumeControl.setMax(maxVolume);
    noiseVolumeControl.setProgress(curVolume);
    SeekBar rainVolumeControl = (SeekBar) findViewById(R.id.rainSeekBar);
    rainVolumeControl.setMax(maxVolume);
    rainVolumeControl.setProgress(curVolume);

    noiseVolumeControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    rainVolumeControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

}

这只控制主音量,我希望能够降低音量,然后提升另一音量。

谢谢!

1 个答案:

答案 0 :(得分:0)

应该使用SoundPool.setVolume(int id,float leftVol,float rightVol);方法。我没试过,但我猜它一定是:

mySound.setVolume(whiteNoiseId,0.2,0.2);

mySoundPool.setVolume(rainId,0.8,0.8);