nick zoum nick zoum - 24 days ago 15
Java Question

Set volume of Java Clip

Is there any way to set the respective volume of a

Clip
in
Java
?

I have this method:

public static void play(Clip clip) {
if (Settings.getSettings().isVolumeOn()) {
FloatControl volume = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(-1 * Settings.getSettings().getVolume());
clip.start();
}
}


The
Settings.getSettings().getVolume()
returns an
Integer
in the range of
0 - 100



Volumes:


  1. 0 : No Sound

  2. 40 : Optimal Sound with headphones

  3. 60 : Optimal Sound

  4. 100: Full Sound




So essentially this should be like the scale of
VLC
(but half since VLC is from
0 to 200
).

I've found that I can reduce the decibel of the clip by using
volume.setValue(-10f);


But I would prefer something of the type
volume.setValue(clip.getMaxVolume() * Settings.getSettings().getVolume()/100)
.

Where
clip.getMaxVolume()
would return the max volume of the clip.

Answer

According to Killer Game Programming by Andrew Davison you can adjust volume this simple:

float range = gainControl.getMaximum() - gainControl.getMinimum();
float gain = (range * volume) + gainControl.getMinimum();
gainControl.setValue(gain);

volume being the desired volume in float (0.0f means no sound, 1.0f means full audio) gainControl is the FloatControl

Hope you can get it to work!

Comments