SoundPool individual Volume adjust causes audio distortion

Viewed 98

Using SoundPool and AudioManager to adjust each sound's volume individually. The sounds work fine at the first volume adjustment. However, on the second try of adjusting the same sound's volume seekbar, the sound starts to distorted when I try to adjust it back from low to high volume. Each sound is supposed to play on loop when I adjust the volume using seekbar.

How to prevent audio distortion?

The code below are my MainActivity

package com.fyp.playmultipleaudio;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private int sound1, sound2, sound3, sound4, sound5, sound6;
    private double sound1Vol, sound2Vol, sound3Vol;
    AudioManager audioManager;
    SoundPool soundPool;
private int getSound1, getSound2, getSound3, getSound4, getSound5, getSound6;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build();
        soundPool = new SoundPool.Builder()
                .setMaxStreams(6)
                .setAudioAttributes(audioAttributes)
                .build();
    } else {
        soundPool = new SoundPool(6, AudioManager.STREAM_MUSIC, 0);
    }

    sound1 = soundPool.load(this, R.raw.wind_chimes, 1);
    sound2 = soundPool.load(this, R.raw.bells, 1);
    sound3 = soundPool.load(this, R.raw.fire, 1);
    sound4 = soundPool.load(this, R.raw.rain, 1);
    sound5 = soundPool.load(this, R.raw.crickets, 1);
    sound6 = soundPool.load(this, R.raw.water, 1);

    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    //Get max volume
    int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

    //Get current volume
    int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);


    SeekBar seekBar1 = findViewById(R.id.seekBar1);
    seekBar1.setMax(maxVolume);
    seekBar1.setProgress(curVolume);

    seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            sound1Vol = (double) progress / (double) maxVolume;
            getSound1 = soundPool.play(sound1, (float) sound1Vol, (float) sound1Vol, 1, 1, 1);
            soundPool.setVolume(getSound1, (float) sound1Vol, (float) sound1Vol);

            /*Log.i("Seekbar changed", Integer.toString(progress));
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0); */

        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    });

    SeekBar seekBar2 = findViewById(R.id.seekBar2);
    seekBar2.setMax(maxVolume);
    seekBar2.setProgress(curVolume);

    seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            sound2Vol = (double) progress / (double) maxVolume;
            getSound2 = soundPool.play(sound2, (float) sound2Vol, (float) sound2Vol, 1, 1, 1);
            soundPool.setVolume(getSound2, (float) sound2Vol, (float) sound2Vol);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    SeekBar seekBar3 = findViewById(R.id.seekBar3);
    seekBar3.setMax(maxVolume);
    seekBar3.setProgress(curVolume);

    seekBar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            sound3Vol = (double) progress / (double) maxVolume;
            getSound3 = soundPool.play(sound3, (float) sound3Vol, (float) sound3Vol, 1, 1, 1);
            soundPool.setVolume(getSound3, (float) sound3Vol, (float) sound3Vol);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
}

public void playSound(View v) {
    switch (v.getId()) {
        case R.id.button_sound1:
            soundPool.stop(getSound1);
            break;
        case R.id.button_sound2:
            soundPool.stop(getSound2);
            break;
        case R.id.button_sound3:
            soundPool.stop(getSound3);
            break;
    }
}
@Override
protected void onDestroy() {
    super.onDestroy();
    soundPool.release();
    soundPool = null;
}
}
1 Answers

You should set progress for your seekbar

Related