How can I play sound on a specified volume in Java?

I already have a working method for playing sound effects in my game, but all sounds play by default. I want the sound to be quieter if the distance between the sound source and the player increases.

I am currently using:

public static void play(String sound) {
    Clip clip = soundList.get(sound);
    clip.setFramePosition(0);
    //I want to set the clip volume here
    clip.start();
}

There is something like clip.getLevel () but no setter. How to adjust the volume of a clip before starting it?

Thanks in advance!

+3
source share
2 answers

In Java 7 (and possibly Java 6), there is a set of classes that give clip control . It:

  • Logical management
    • Mute
    • Apply / Remove Reverb
  • Compound Control
  • Transfer management
    • Reverb Elements
    • GAIN
    • VOLUME

, , FloatControl. , . , :

FloatControl volume = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(gainAmount);

-OR -

((FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN)).setValue(gainAmount)

gainAmount - , ( ) .

+1

API- , AFAIK. , , . , , , , .5.

- , "". .

0

All Articles