I build a JLayer that plays a web music stream, which works fine, except for the volume. I cant get it to change.
I already tried the following, but the volume always remains the same, no errors:
public synchronized void setVolume(float vol) {
Line.Info source = Port.Info.SPEAKER;
if (AudioSystem.isLineSupported(source)) {
try {
Port outline = (Port) AudioSystem.getLine(source);
outline.open();
FloatControl volumeControl = (FloatControl) outline.getControl(FloatControl.Type.VOLUME);
System.out.println("volume: " + volumeControl.getValue());
volumeControl.setValue(vol);
System.out.println("new volume: " + volumeControl.getValue());
} catch (LineUnavailableException ex) {
System.err.println("source not supported");
ex.printStackTrace();
}
}
}
/*public synchronized void setVolume(double vol) {
try {
Mixer.Info[] infos = AudioSystem.getMixerInfo();
for (Mixer.Info info : infos) {
Mixer mixer = AudioSystem.getMixer(info);
if (mixer.isLineSupported(Port.Info.SPEAKER)) {
Port port = (Port) mixer.getLine(Port.Info.SPEAKER);
port.open();
if (port.isControlSupported(FloatControl.Type.VOLUME)) {
FloatControl volume = (FloatControl) port.getControl(FloatControl.Type.VOLUME);
volume.setValue((float) (vol / 100));
}
port.close();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}*/
My experience is that the implementation of the various
Controlssupported by Java is somewhat hit-or-miss. I'm not clear why this should be. Perhaps it has to do with the capabilities supported by the OS or particular machine?In any event, for volume, the MASTER_GAIN is usually the best bet for volume.
The ending portion of the Java Tutorials section on using Controls discusses manipulating the audio data directly. I recommend this option, but getting at the data when using the JavaZoom code can be problematic, and probably involves tweaking the source code of the library.