Volume control in a Java application

I am new to java. I need to control the volume (volume up / down, mute) in a Java application. I could not find a way to do this. I am developing a Linux system (for information).

I am tired of this code:

Java Code:

Port lineIn;
FloatControl volCtrl;
try {
  mixer = AudioSystem.getMixer(null);
  lineIn = (Port)mixer.getLine(Port.Info.LINE_IN);
  lineIn.open();
  volCtrl = (FloatControl) lineIn.getControl(

      FloatControl.Type.VOLUME);
  // Assuming getControl call succeeds, 
  // we now have our LINE_IN VOLUME control.
} catch (Exception e) {
  System.out.println("Failed trying to find LINE_IN"
    + " VOLUME control: exception = " + e);
}

but i got exec

Failed trying to find LINE_IN VOLUME control: exception = java.lang.IllegalArgumentException: Line unsupported: COMPACT_DISC source port

thanks for the help

+5
source share
2 answers

Sometimes the controls are nested, which makes a platform-independent solution difficult. I wrote a utility class that is used as follows:

Audio.setMasterOutputVolume(0.5f);

Source code here: https://github.com/Kunagi/ilarkesto/blob/master/src/main/java/ilarkesto/media/Audio.java

Stackoverflow users can use the code from Audio.java in accordance with the WTFPL terms.

+4

, , VOLUME. MASTER_GAIN.

volCtrl = (floatControl) lineIn.getControl(FloatControl.Type.MASTER_GAIN);
volCtrl.setValue(you_custom_value);
-1

All Articles