栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

可以使用Java Sound来控制系统音量吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

可以使用Java Sound来控制系统音量吗?

不,它不能。这是从“ 调整码位上的主音量”的答案改编而来的资源。源代码会迭代可用的行,检查它们是否具有正确类型的控件,如果有,则将它们放入连接到命令行的GUI中。

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;    }}

在这台Windows 7计算机上,我有两个控件,都来自“ Java Sound Audio Engine”。都不会影响当前的系统容量。

run:There are 4 mixer info objectsmixer 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 Gainmixer 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 Gainmixer 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.0mixer name: Port Speakers (VIA High Definition ASetting volume of Master Gain with current value: 0.0 dB (range: -80.0 - 13.9794) to 0.0Setting volume of Master Gain with current value: 0.0 dB (range: -80.0 - 13.9794) to -0.41Setting volume of Master Gain with current value: 0.0 dB (range: -80.0 - 13.9794) to -0.68...

交换

FloatControl.Type.MASTER_GAIN
FloatControl.Type.VOLUME
看..没有控制。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/486247.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号