【问题标题】:SoundPool individual Volume adjust causes audio distortionSoundPool 单独音量调节导致音频失真
【发布时间】:2021-05-30 10:48:57
【问题描述】:

使用 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;
}
}

【问题讨论】:

    标签: java android-studio seekbar android-audiomanager soundpool


    【解决方案1】:

    你应该为你的搜索栏设置进度

    【讨论】:

    • 你能解释一下怎么做吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    相关资源
    最近更新 更多