Controlling volume in a java application

纵饮孤独 提交于 2019-12-19 19:12:53

问题


I am fairly new at java. I need to control volume (volume up/down, mute) in a java application. I couldn't find a way to do this. I am developing on a linux system (for information).

I tired 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 execption

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

Thanks for your help


回答1:


Sometimes the controls are nested, which makes a platform independent solution difficult. I have written an utility class which is used like that:

Audio.setMasterOutputVolume(0.5f);

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

Stackoverflow users can use the code from Audio.java under the terms of the WTFPL.




回答2:


I don't think there is a constant called VOLUME. Its MASTER_GAIN. So you should do

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


来源:https://stackoverflow.com/questions/7956115/controlling-volume-in-a-java-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!