Skip to content

setVolumeFactor ​

Set Volume Scaling Factor ​

setVolumeFactor is the core method in the SUDRTGameAudioSession interface for adjusting the game volume level. By changing the gain value of the audio output, it achieves precise control of the game's internal sound level without touching the global system media volume.


Method Declaration ​

java
/**
 * Set the volume scaling ratio of the current game instance.
 * @param var1 The volume factor, ranging from 0.0f (completely silent) to 1.0f (full volume).
 * Values outside this range are usually automatically clamped by the SDK to the [0, 1] interval.
 */
void setVolumeFactor(float var1);

Runtime Behavior ​

  1. Relative Gain: This method adjusts a relative ratio. Actual perceived volume = Android system media volume × var1.
  2. Immediate Effect: After the call, the currently playing background music (BGM) and real-time sound effects (SFX) change volume immediately, without restarting the audio stream.
  3. Non-destructive: Lowering this value does not affect the volume of other apps on the phone (such as a music player running in the background), achieving in-app audio isolation.

Code Example ​

Implement a volume SeekBar to control the game volume:

java
// Get the audio session
SUDRTGameAudioSession audioSession = mGameHandle.getAudioSession();

// SeekBar progress change listener (assuming range 0-100)
volumeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        // Convert 0-100 to a 0.0-1.0 float
        float factor = progress / 100.0f;
        
        // Call the SDK to set the volume factor
        audioSession.setVolumeFactor(factor);
        
        Log.d("SudAudio", "Adjusted game volume factor to: " + factor);
    }
});

Notes ​

  • Smoothing: Although the SDK supports setting directly, if the App side calls it at high frequency in a short time (such as in an animation loop), it is recommended to apply some throttling to ensure smooth audio transitions.
  • Conflict with Mute: If mute(true) has already been called, the user will hear no sound regardless of the value set by setVolumeFactor.
  • Default Level: Most SDKs default the volume factor to 1.0f at initialization.