Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 362
mikera Avatar asked Aug 16 '10 17:08

mikera


People also ask

How do you play a sound clip in Java?

Following steps are to be followed to play a clip object. getAudioInputStream(File file). AudioInputStream converts an audio file into stream. Get a clip reference object from AudioSystem. Stream an audio input stream from which audio data will be read into the clip by using open() method of Clip interface.


1 Answers

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.

like image 141
qmega Avatar answered Sep 30 '22 09:09

qmega