Controlling volume of a Clip when using Java Sound (javax,sound.sampled)

mikera picture mikera · Aug 16, 2010 · Viewed 8.1k times · Source

I'm using Java sound to play back a number of sound samples with the Clip.start() method.

Given this, what is the best way to control the playback volume?

In particular, I'm very keen that the solution will work reliably across platforms.

Answer

qmega picture qmega · Aug 16, 2010
FloatControl volume = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(gainAmount);

Just replace gainAmount with a float representing the gain in decibels. Can be positive or negative.