How to control soundpool volume?

67 views Asked by At

Im trying to change the soundpool volume with seekbar, but tutoruals that I have seen on the intrenet do nothing to the soundpool.

I tried playing with AudioManeger and the soundpool setVolume function but it doesn't seem to affect anything.

I don't know what im doing wrong and I will really apriciate the help. Thank you.

this is my code:

package com.example.kfir_compare;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

public class Sound extends AppCompatActivity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener{


    public SoundPool soundPool;
    private int correctSound;
    private int incorrectSound;
    private int correctSoundV;
    private int incorrectSoundV;

    private AudioManager audioManager;
    private int maxVol;
    private int curVol;
    private float volume;

    private Button soundB;
    private Button voiceB;

    private Button bb;
    private SharedPreferences sp;

    private SeekBar seekBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sound);
        initElements();
        initializeSoundPool();

        if (getIntent().getExtras() != null){
            Intent intent = new Intent();
            intent.putExtra("sound", 2);
            setResult(2, intent); // number param for your choice
            finish(); // to destroy the second activity


        }

        //audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        //curVol = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        //maxVol = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
       // seekBar.setMax(maxVol);
        //seekBar.setProgress(curVol);



    }

    public void initializeSoundPool() {
        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(1)
                    .setAudioAttributes(audioAttributes)
                    .build();
        }
        else {
            soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
        }
        correctSound = soundPool.load(this, R.raw.correctsound, 1);
        incorrectSound = soundPool.load(this, R.raw.wrongsound, 1);
        correctSoundV = soundPool.load(this, R.raw.youarecorrectadiofile, 1);
        incorrectSoundV = soundPool.load(this, R.raw.youarewrongadiofile, 1);
    }





    private void initElements() {

        soundB = findViewById(R.id.NS);
        soundB.setOnClickListener(this);

        voiceB = findViewById(R.id.VS);
        voiceB.setOnClickListener(this);

        bb = findViewById(R.id.BB);
        bb.setOnClickListener(this);

        sp = getSharedPreferences("sound", 0);
        //seekBar = findViewById(R.id.SeekBar);

       // volume = 1;
    }


    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, MainActivity.class);
        if (v == soundB){
            soundPool.play(correctSound,1,1,0,0,1);
            SharedPreferences.Editor editor = sp.edit();
            editor.putInt("sound", 2);
            editor.apply();
        }

        else if(v == voiceB) {
            soundPool.play(correctSoundV,1,1,0,0,1);
            SharedPreferences.Editor editor = sp.edit();
            editor.putInt("sound", 3);
            editor.apply();
        }
        else if (v.getId() == R.id.BB) {
            setResult(2, intent); // number param for your choice
            finish(); // to destroy the second activity
        }


    }

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

       // soundPool.setVolume(correctSound, (float) (progress/100.0), (float) (progress/100.0));
       // soundPool.setVolume(correctSoundV, (float) (progress/100.0), (float) (progress/100.0));
       // soundPool.setVolume(incorrectSound, (float) (progress/100.0), (float) (progress/100.0));
      //  soundPool.setVolume(incorrectSoundV, (float) (progress/100.0), (float) (progress/100.0));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}


0

There are 0 answers