1

可能重复:
控制 Java 应用程序中的音量

我正在寻求帮助,如何从应用程序中控制计算机的音量。我已经在其他帖子中看到了示例,例如:

Port lineIn = null;
    FloatControl volCtrl = null;
    try {
        Mixer mixer = AudioSystem.getMixer(null);
        lineIn = (Port) mixer.getLine(Port.Info.LINE_IN);
        lineIn.open();
        volCtrl = (FloatControl) lineIn
                .getControl(FloatControl.Type.MASTER_GAIN);
        volCtrl.setValue(volCtrl.getValue() + 10f);
    } catch (Exception e) {
        System.out.println("Failed trying to find LINE_IN"
                + " VOLUME control: exception = " + e);
        lineIn.close();
    }

此示例取自Oracle页面但它引发异常...

java.lang.IllegalArgumentException: Line unsupported: LINE_IN source port
at com.sun.media.sound.DirectAudioDevice.getLine(Unknown Source)
at core.ProtocolHandler.volumeUp(ProtocolHandler.java:223)
at core.ProtocolHandler.answer(ProtocolHandler.java:211)
at core.Server.run(Server.java:77)
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
at core.ProtocolHandler.volumeUp(ProtocolHandler.java:230)
at core.ProtocolHandler.answer(ProtocolHandler.java:211)
at core.Server.run(Server.java:77)
at java.lang.Thread.run(Unknown Source)

我也试过这个例子......但它给我带来了同样的异常。

4

0 回答 0