📄 javamixer.java.svn-base
字号:
/**
* $Revision: $
* $Date: $
*
* Copyright (C) 2006 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Lesser Public License (LGPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.sparkplugin;
import javax.sound.sampled.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeNode;
import javax.swing.*;
import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import java.util.List;
import java.util.ArrayList;
import java.util.Enumeration;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
* Pure Java Audio Mixer. Control Volume and Settings for any Sound device in the OS.
*
* @author Thiago Camargo
*/
public class JavaMixer {
private static final Line.Info[] EMPTY_PORT_INFO_ARRAY = new Line.Info[0];
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Sound Mixers", true);
JTree tree = new JTree(root);
public JavaMixer() {
List<Mixer> portMixers = getPortMixers();
if (portMixers.size() == 0) System.err.println("No Mixers Found.");
for (Mixer mixer : portMixers) {
JavaMixer.MixerNode mixerNode = new JavaMixer.MixerNode(mixer);
createMixerChildren(mixerNode);
root.add(mixerNode);
}
}
public JTree getTree() {
return tree;
}
public Component getPrefferedMasterVolume() {
TreePath path = findByName(new TreePath(root), new String[]{"SPEAKER", "Volume"});
if (path == null) {
path = findByName(new TreePath(root), new String[]{"Master target", "Master", "Mute"});
}
if (path != null) {
if (path.getLastPathComponent() instanceof JavaMixer.ControlNode)
return ((JavaMixer.ControlNode) path.getLastPathComponent()).getComponent();
}
return null;
}
public Component getPrefferedInputVolume() {
TreePath path = findByName(new TreePath(root), new String[]{"MICROPHONE", "Volume"});
if (path == null) {
path = findByName(new TreePath(root), new String[]{"Capture source", "Capture", "Volume"});
}
if (path != null) {
if (path.getLastPathComponent() instanceof JavaMixer.ControlNode)
return ((JavaMixer.ControlNode) path.getLastPathComponent()).getComponent();
}
return null;
}
public void setMicrophoneInput() {
TreePath path = findByName(new TreePath(root), new String[]{"MICROPHONE", "Select"});
if (path == null) {
path = findByName(new TreePath(root), new String[]{"Capture source", "Capture", "Mute"});
}
if (path != null) {
if (path.getLastPathComponent() instanceof JavaMixer.ControlNode) {
BooleanControl bControl = (BooleanControl) (((JavaMixer.ControlNode) path.getLastPathComponent()).getControl());
bControl.setValue(true);
}
}
}
public void setMuteForMicrophoneOutput() {
TreePath path = findByName(new TreePath(root), new String[]{"SPEAKER", "Microfone", "Mute"});
if (path == null) {
path = findByName(new TreePath(root), new String[]{"MIC target", "mic", "Mute"});
}
if (path != null) {
if (path.getLastPathComponent() instanceof JavaMixer.ControlNode) {
BooleanControl bControl = (BooleanControl) (((JavaMixer.ControlNode) path.getLastPathComponent()).getControl());
bControl.setValue(true);
}
}
}
/**
* Returns the Mixers that support Port lines.
*
* @return List<Mixer> Port Mixers
*/
private List<Mixer> getPortMixers() {
List<Mixer> supportingMixers = new ArrayList<Mixer>();
Mixer.Info[] aMixerInfos = AudioSystem.getMixerInfo();
for (Mixer.Info aMixerInfo : aMixerInfos) {
Mixer mixer = AudioSystem.getMixer(aMixerInfo);
boolean bSupportsPorts = arePortsSupported(mixer);
if (bSupportsPorts) {
supportingMixers.add(mixer);
}
}
return supportingMixers;
}
private boolean arePortsSupported(Mixer mixer) {
Line.Info[] infos;
infos = mixer.getSourceLineInfo();
for (Line.Info info : infos) {
if (info instanceof Port.Info) {
return true;
} else if (info instanceof DataLine.Info) {
return true;
}
}
infos = mixer.getTargetLineInfo();
for (Line.Info info : infos) {
if (info instanceof Port.Info) {
return true;
} else if (info instanceof DataLine.Info) {
return true;
}
}
return false;
}
private void createMixerChildren(JavaMixer.MixerNode mixerNode) {
Mixer mixer = mixerNode.getMixer();
Line.Info[] infosToCheck = getPortInfo(mixer);
for (Line.Info anInfosToCheck : infosToCheck) {
if (mixer.isLineSupported(anInfosToCheck)) {
Port port = null;
DataLine dLine = null;
try {
if (anInfosToCheck instanceof Port.Info) {
port = (Port) mixer.getLine(anInfosToCheck);
port.open();
} else if (anInfosToCheck instanceof DataLine.Info) {
dLine = (DataLine) mixer.getLine(anInfosToCheck);
dLine.open();
}
}
catch (LineUnavailableException e) {
e.printStackTrace();
} catch (Exception e) {
// Do Nothing
}
if (port != null) {
JavaMixer.PortNode portNode = new JavaMixer.PortNode(port);
createPortChildren(portNode);
mixerNode.add(portNode);
} else if (dLine != null) {
JavaMixer.PortNode portNode = new JavaMixer.PortNode(dLine);
createPortChildren(portNode);
mixerNode.add(portNode);
}
}
}
}
private Line.Info[] getPortInfo(Mixer mixer) {
Line.Info[] infos;
List<Line.Info> portInfoList = new ArrayList<Line.Info>();
infos = mixer.getSourceLineInfo();
for (Line.Info info : infos) {
if (info instanceof Port.Info || info instanceof DataLine.Info) {
portInfoList.add((Line.Info) info);
}
}
infos = mixer.getTargetLineInfo();
for (Line.Info info1 : infos) {
if (info1 instanceof Port.Info || info1 instanceof DataLine.Info) {
portInfoList.add((Line.Info) info1);
}
}
return portInfoList.toArray(EMPTY_PORT_INFO_ARRAY);
}
private void createPortChildren(JavaMixer.PortNode portNode) {
Control[] aControls = portNode.getPort().getControls();
for (Control aControl : aControls) {
JavaMixer.ControlNode controlNode = new JavaMixer.ControlNode(aControl);
createControlChildren(controlNode);
portNode.add(controlNode);
}
}
private void createControlChildren(JavaMixer.ControlNode controlNode) {
if (controlNode.getControl() instanceof CompoundControl) {
CompoundControl control = (CompoundControl) controlNode.getControl();
Control[] aControls = control.getMemberControls();
for (Control con : aControls) {
JavaMixer.ControlNode conNode = new JavaMixer.ControlNode(con);
createControlChildren(conNode);
controlNode.add(conNode);
}
}
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -