控制java应用程序中的卷 [英] Controlling volume in a java application
问题描述
我是java的新手。我需要在java应用程序中控制音量(音量增大/减小,静音)。我找不到办法做到这一点。我正在开发一个Linux系统(供参考)。
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).
我厌倦了这段代码:
Java代码:
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);
}
但是我得到了执行
Failed trying to find LINE_IN VOLUME control: exception = java.lang.IllegalArgumentException: Line unsupported: COMPACT_DISC source port
感谢您的帮助
推荐答案
有时控件是嵌套的,这使得平台无关的解决方案很难。我编写了一个实用类,使用如下:
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);
源代码在这里: https://github.com/Kunagi/ilarkesto/blob/master/src/main/java/ilarkesto/media /Audio.java
Stackoverflow用户可以根据WTFPL的条款使用Audio.java中的代码。
Stackoverflow users can use the code from Audio.java under the terms of the WTFPL.
这篇关于控制java应用程序中的卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!