5

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.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
mikera
  • 105,238
  • 25
  • 256
  • 415

2 Answers2

12
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.

qmega
  • 1,243
  • 9
  • 9
  • what does a negative value? o.O – T_01 Aug 14 '13 at 22:54
  • 5
    @T_01 -3 means 2 times less, -6 means 4 times less, -10 means 10 times less etc. (logarithmic scale) See [Wikipedia](http://en.wikipedia.org/wiki/Decibel) – Demurgos Feb 23 '15 at 21:00
0
  • adds the amount to your volume:
FloatControl volume = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(volume.getValue()+gainAmount)
  • sets volume to a certain amount from -80.0 to 6.0206:
FloatControl volume = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(gainAmount);
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 10 '21 at 19:13