SoundPool单独的音量调整会导致音频失真



使用SoundPool和AudioManager分别调整每个声音的音量。第一次调节音量时声音很好。然而,在第二次尝试调整相同声音的音量时,当我试图将其从低音量调整回高音量时,声音开始失真。当我使用seekbar调节音量时,每个声音都应该循环播放。

如何防止音频失真?

下面的代码是我的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;
}
}

您应该为搜索栏设置进度

最新更新