java实现对音量的控制,可Java的声音被用来控制系统音量?

Java Sound offers FloatControl instances for various sound line functionality, and both a MASTER_GAIN & VOLUME control type.

Can these controls be used to change the system volume?

解决方案

No, it cannot. Here is source adapted from an answer to Adjusting master volume on coderanch. The source iterates the available lines, checks if they have a control of the right type, and if so, puts them in a GUI attached to a JSlider

import java.awt.*;

import javax.swing.*;

import javax.sound.sampled.*;

import javax.swing.event.ChangeEvent;

import javax.swing.event.ChangeListener;

public class SoundMixer {

public Component getGui() {

JPanel gui = new JPanel(new GridLayout(0,1));

Mixer.Info[] mixers = AudioSystem.getMixerInfo();

System.out.println(

"There are " + mixers.length + " mixer info objects");

for (Mixer.Info mixerInfo : mixers) {

System.out.println("mixer name: " + mixerInfo.getName());

Mixer mixer = AudioSystem.getMixer(mixerInfo);

Line.Info[] lineInfos = mixer.getSourceLineInfo();

for (Line.Info lineInfo : lineInfos) {

System.out.println(" Line.Info: " + lineInfo);

try {

Line line = mixer.getLine(lineInfo);

FloatControl volCtrl = (FloatControl)line.getControl(

FloatControl.Type.MASTER_GAIN);

VolumeSlider vs = new VolumeSlider(volCtrl);

gui.add( new JLabel(volCtrl.toString()) );

gui.add( vs.getVolume() );

System.out.println(

" volCtrl.getValue() = " + volCtrl.getValue());

} catch (LineUnavailableException e) {

e.printStackTrace();

} catch (IllegalArgumentException iaEx) {

System.out.println(" " + iaEx);

}

}

}

return gui;

}

public static void main(String[] args) {

Runnable r = new Runnable() {

@Override

public void run() {

SoundMixer sm = new SoundMixer();

Component c = sm.getGui();

JOptionPane.showMessageDialog(null, c);

}

};

// Swing GUIs should be created and updated on the EDT

// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html

SwingUtilities.invokeLater(r);

}

}

class VolumeSlider {

JSlider volume;

VolumeSlider(final FloatControl volumeControl) {

volume = new JSlider(

(int) volumeControl.getMinimum() * 100,

(int) volumeControl.getMaximum() * 100,

(int) volumeControl.getValue() * 100);

ChangeListener listener = new ChangeListener() {

@Override

public void stateChanged(ChangeEvent e) {

float val = volume.getValue() / 100f;

volumeControl.setValue(val);

System.out.println(

"Setting volume of " + volumeControl.toString() +

" to " + val);

}

};

volume.addChangeListener(listener);

}

public JSlider getVolume() {

return volume;

}

}

On this Windows 7 machine I get two controls, both from the "Java Sound Audio Engine". Neither has any effect on the current system volume.

run:

There are 4 mixer info objects

mixer name: Primary Sound Driver

Line.Info: interface SourceDataLine supporting 8 audio formats, and buffers of at least 32 bytes

java.lang.IllegalArgumentException: Unsupported control type: Master Gain

Line.Info: interface Clip supporting 8 audio formats, and buffers of at least 32 bytes

java.lang.IllegalArgumentException: Unsupported control type: Master Gain

mixer name: Speakers (VIA High Definition Audio)

Line.Info: interface SourceDataLine supporting 8 audio formats, and buffers of at least 32 bytes

java.lang.IllegalArgumentException: Unsupported control type: Master Gain

Line.Info: interface Clip supporting 8 audio formats, and buffers of at least 32 bytes

java.lang.IllegalArgumentException: Unsupported control type: Master Gain

mixer name: Java Sound Audio Engine

Line.Info: interface SourceDataLine supporting 8 audio formats

volCtrl.getValue() = 0.0

Line.Info: interface Clip supporting 8 audio formats, and buffers of 0 to 4194304 bytes

volCtrl.getValue() = 0.0

mixer name: Port Speakers (VIA High Definition A

Setting volume of Master Gain with current value: 0.0 dB (range: -80.0 - 13.9794) to 0.0

Setting volume of Master Gain with current value: 0.0 dB (range: -80.0 - 13.9794) to -0.41

Setting volume of Master Gain with current value: 0.0 dB (range: -80.0 - 13.9794) to -0.68

...

Swap FloatControl.Type.MASTER_GAIN for FloatControl.Type.VOLUME to see.. no controls.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Java的AudioSystem类来控制系统音量。具体实现步骤如下: 1. 首先,你需要获取到系统的音量控制对象,可以通过AudioSystem类的静态方法getMixerInfo()获取到所有可用的音频设备信息,然后通过Mixer类的getLine()方法获取到对应的Line对象。 2. 然后,你需要检查获取到的Line对象是否是一个支持音量控制的对象。你可以通过Line类的isControlSupported()方法和getControls()方法获取到该对象支持的所有控制器。 3. 如果该对象支持音量控制,你可以通过查找到的控制器对象来控制音量。一般来说,音量控制器的类型是VolumeControl或者MasterGainControl。你可以通过查看控制器对象的类名来确定其类型。 4. 最后,你可以使用控制器对象提供的setValue()方法来设置音量大小。 下面是一个简单的示例代码,可以将系统音量设置为50%: ```java import javax.sound.sampled.*; public class VolumeControl { public static void main(String[] args) throws Exception { Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo(); for (Mixer.Info info : mixerInfos) { Mixer mixer = AudioSystem.getMixer(info); Line.Info[] lineInfos = mixer.getTargetLineInfo(); for (Line.Info lineInfo : lineInfos) { Line line = mixer.getLine(lineInfo); if (line instanceof VolumeControl || line instanceof MasterGainControl) { Control[] controls = line.getControls(); for (Control control : controls) { if (control instanceof FloatControl) { FloatControl floatControl = (FloatControl) control; if (floatControl.getType() == FloatControl.Type.VOLUME) { floatControl.setValue(0.5f); } } } } } } } } ``` 需要注意的是,由于每个操作系统的音频设备和控制器可能有所不同,上述代码可能不能完全适用于所有操作系统和设备。你需要根据实际情况进行调整和测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值