下面列出了怎么用javax.sound.sampled.Port的API类实例代码及写法,或者点击链接到github查看源代码。
private Port.Info getPortInfo(int portIndex, int type) {
switch (type) {
case SRC_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), true);
case SRC_MICROPHONE: return Port.Info.MICROPHONE;
case SRC_LINE_IN: return Port.Info.LINE_IN;
case SRC_COMPACT_DISC: return Port.Info.COMPACT_DISC;
case DST_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), false);
case DST_SPEAKER: return Port.Info.SPEAKER;
case DST_HEADPHONE: return Port.Info.HEADPHONE;
case DST_LINE_OUT: return Port.Info.LINE_OUT;
}
// should never happen...
if (Printer.debug) Printer.debug("unknown port type: "+type);
return null;
}
/** Obtain the value of a default provider property.
@param typeClass The type of the default provider property. This
should be one of Receiver.class, Transmitter.class, Sequencer.class,
Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
Clip.class or Port.class.
@return The complete value of the property, if available.
If the property is not set, null is returned.
*/
private static synchronized String getDefaultProvider(Class typeClass) {
if (!SourceDataLine.class.equals(typeClass)
&& !TargetDataLine.class.equals(typeClass)
&& !Clip.class.equals(typeClass)
&& !Port.class.equals(typeClass)
&& !Receiver.class.equals(typeClass)
&& !Transmitter.class.equals(typeClass)
&& !Synthesizer.class.equals(typeClass)
&& !Sequencer.class.equals(typeClass)) {
return null;
}
String name = typeClass.getName();
String value = AccessController.doPrivileged(
(PrivilegedAction<String>) () -> System.getProperty(name));
if (value == null) {
value = getProperties().getProperty(name);
}
if ("".equals(value)) {
value = null;
}
return value;
}
private Port.Info getPortInfo(int portIndex, int type) {
switch (type) {
case SRC_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), true);
case SRC_MICROPHONE: return Port.Info.MICROPHONE;
case SRC_LINE_IN: return Port.Info.LINE_IN;
case SRC_COMPACT_DISC: return Port.Info.COMPACT_DISC;
case DST_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), false);
case DST_SPEAKER: return Port.Info.SPEAKER;
case DST_HEADPHONE: return Port.Info.HEADPHONE;
case DST_LINE_OUT: return Port.Info.LINE_OUT;
}
// should never happen...
if (Printer.debug) Printer.debug("unknown port type: "+type);
return null;
}
private Port.Info getPortInfo(int portIndex, int type) {
switch (type) {
case SRC_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), true);
case SRC_MICROPHONE: return Port.Info.MICROPHONE;
case SRC_LINE_IN: return Port.Info.LINE_IN;
case SRC_COMPACT_DISC: return Port.Info.COMPACT_DISC;
case DST_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), false);
case DST_SPEAKER: return Port.Info.SPEAKER;
case DST_HEADPHONE: return Port.Info.HEADPHONE;
case DST_LINE_OUT: return Port.Info.LINE_OUT;
}
// should never happen...
if (Printer.debug) Printer.debug("unknown port type: "+type);
return null;
}
/** Obtain the value of a default provider property.
@param typeClass The type of the default provider property. This
should be one of Receiver.class, Transmitter.class, Sequencer.class,
Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
Clip.class or Port.class.
@return The complete value of the property, if available.
If the property is not set, null is returned.
*/
private static synchronized String getDefaultProvider(Class typeClass) {
if (!SourceDataLine.class.equals(typeClass)
&& !TargetDataLine.class.equals(typeClass)
&& !Clip.class.equals(typeClass)
&& !Port.class.equals(typeClass)
&& !Receiver.class.equals(typeClass)
&& !Transmitter.class.equals(typeClass)
&& !Synthesizer.class.equals(typeClass)
&& !Sequencer.class.equals(typeClass)) {
return null;
}
String name = typeClass.getName();
String value = AccessController.doPrivileged(
(PrivilegedAction<String>) () -> System.getProperty(name));
if (value == null) {
value = getProperties().getProperty(name);
}
if ("".equals(value)) {
value = null;
}
return value;
}
/** Obtain the value of a default provider property.
@param typeClass The type of the default provider property. This
should be one of Receiver.class, Transmitter.class, Sequencer.class,
Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
Clip.class or Port.class.
@return The complete value of the property, if available.
If the property is not set, null is returned.
*/
private static synchronized String getDefaultProvider(Class typeClass) {
if (!SourceDataLine.class.equals(typeClass)
&& !TargetDataLine.class.equals(typeClass)
&& !Clip.class.equals(typeClass)
&& !Port.class.equals(typeClass)
&& !Receiver.class.equals(typeClass)
&& !Transmitter.class.equals(typeClass)
&& !Synthesizer.class.equals(typeClass)
&& !Sequencer.class.equals(typeClass)) {
return null;
}
String value;
String propertyName = typeClass.getName();
value = JSSecurityManager.getProperty(propertyName);
if (value == null) {
value = getProperties().getProperty(propertyName);
}
if ("".equals(value)) {
value = null;
}
return value;
}
private Port.Info getPortInfo(int portIndex, int type) {
switch (type) {
case SRC_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), true);
case SRC_MICROPHONE: return Port.Info.MICROPHONE;
case SRC_LINE_IN: return Port.Info.LINE_IN;
case SRC_COMPACT_DISC: return Port.Info.COMPACT_DISC;
case DST_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), false);
case DST_SPEAKER: return Port.Info.SPEAKER;
case DST_HEADPHONE: return Port.Info.HEADPHONE;
case DST_LINE_OUT: return Port.Info.LINE_OUT;
}
// should never happen...
if (Printer.debug) Printer.debug("unknown port type: "+type);
return null;
}
private Port.Info getPortInfo(int portIndex, int type) {
switch (type) {
case SRC_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), true);
case SRC_MICROPHONE: return Port.Info.MICROPHONE;
case SRC_LINE_IN: return Port.Info.LINE_IN;
case SRC_COMPACT_DISC: return Port.Info.COMPACT_DISC;
case DST_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), false);
case DST_SPEAKER: return Port.Info.SPEAKER;
case DST_HEADPHONE: return Port.Info.HEADPHONE;
case DST_LINE_OUT: return Port.Info.LINE_OUT;
}
// should never happen...
if (Printer.debug) Printer.debug("unknown port type: "+type);
return null;
}
/** Obtain the value of a default provider property.
@param typeClass The type of the default provider property. This
should be one of Receiver.class, Transmitter.class, Sequencer.class,
Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
Clip.class or Port.class.
@return The complete value of the property, if available.
If the property is not set, null is returned.
*/
private static synchronized String getDefaultProvider(Class typeClass) {
if (!SourceDataLine.class.equals(typeClass)
&& !TargetDataLine.class.equals(typeClass)
&& !Clip.class.equals(typeClass)
&& !Port.class.equals(typeClass)
&& !Receiver.class.equals(typeClass)
&& !Transmitter.class.equals(typeClass)
&& !Synthesizer.class.equals(typeClass)
&& !Sequencer.class.equals(typeClass)) {
return null;
}
String name = typeClass.getName();
String value = AccessController.doPrivileged(
(PrivilegedAction<String>) () -> System.getProperty(name));
if (value == null) {
value = getProperties().getProperty(name);
}
if ("".equals(value)) {
value = null;
}
return value;
}
private Port.Info getPortInfo(int portIndex, int type) {
switch (type) {
case SRC_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), true);
case SRC_MICROPHONE: return Port.Info.MICROPHONE;
case SRC_LINE_IN: return Port.Info.LINE_IN;
case SRC_COMPACT_DISC: return Port.Info.COMPACT_DISC;
case DST_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), false);
case DST_SPEAKER: return Port.Info.SPEAKER;
case DST_HEADPHONE: return Port.Info.HEADPHONE;
case DST_LINE_OUT: return Port.Info.LINE_OUT;
}
// should never happen...
if (Printer.err) Printer.err("unknown port type: "+type);
return null;
}
private Port.Info getPortInfo(int portIndex, int type) {
switch (type) {
case SRC_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), true);
case SRC_MICROPHONE: return Port.Info.MICROPHONE;
case SRC_LINE_IN: return Port.Info.LINE_IN;
case SRC_COMPACT_DISC: return Port.Info.COMPACT_DISC;
case DST_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), false);
case DST_SPEAKER: return Port.Info.SPEAKER;
case DST_HEADPHONE: return Port.Info.HEADPHONE;
case DST_LINE_OUT: return Port.Info.LINE_OUT;
}
// should never happen...
if (Printer.debug) Printer.debug("unknown port type: "+type);
return null;
}
/** Obtain the value of a default provider property.
@param typeClass The type of the default provider property. This
should be one of Receiver.class, Transmitter.class, Sequencer.class,
Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
Clip.class or Port.class.
@return The complete value of the property, if available.
If the property is not set, null is returned.
*/
private static synchronized String getDefaultProvider(Class<?> typeClass) {
if (!SourceDataLine.class.equals(typeClass)
&& !TargetDataLine.class.equals(typeClass)
&& !Clip.class.equals(typeClass)
&& !Port.class.equals(typeClass)
&& !Receiver.class.equals(typeClass)
&& !Transmitter.class.equals(typeClass)
&& !Synthesizer.class.equals(typeClass)
&& !Sequencer.class.equals(typeClass)) {
return null;
}
String name = typeClass.getName();
String value = AccessController.doPrivileged(
(PrivilegedAction<String>) () -> System.getProperty(name));
if (value == null) {
value = getProperties().getProperty(name);
}
if ("".equals(value)) {
value = null;
}
return value;
}
private Port.Info getPortInfo(int portIndex, int type) {
switch (type) {
case SRC_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), true);
case SRC_MICROPHONE: return Port.Info.MICROPHONE;
case SRC_LINE_IN: return Port.Info.LINE_IN;
case SRC_COMPACT_DISC: return Port.Info.COMPACT_DISC;
case DST_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), false);
case DST_SPEAKER: return Port.Info.SPEAKER;
case DST_HEADPHONE: return Port.Info.HEADPHONE;
case DST_LINE_OUT: return Port.Info.LINE_OUT;
}
// should never happen...
if (Printer.debug) Printer.debug("unknown port type: "+type);
return null;
}
/** Obtain the value of a default provider property.
@param typeClass The type of the default provider property. This
should be one of Receiver.class, Transmitter.class, Sequencer.class,
Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
Clip.class or Port.class.
@return The complete value of the property, if available.
If the property is not set, null is returned.
*/
private static synchronized String getDefaultProvider(Class typeClass) {
if (!SourceDataLine.class.equals(typeClass)
&& !TargetDataLine.class.equals(typeClass)
&& !Clip.class.equals(typeClass)
&& !Port.class.equals(typeClass)
&& !Receiver.class.equals(typeClass)
&& !Transmitter.class.equals(typeClass)
&& !Synthesizer.class.equals(typeClass)
&& !Sequencer.class.equals(typeClass)) {
return null;
}
String name = typeClass.getName();
String value = AccessController.doPrivileged(
(PrivilegedAction<String>) () -> System.getProperty(name));
if (value == null) {
value = getProperties().getProperty(name);
}
if ("".equals(value)) {
value = null;
}
return value;
}
private Port.Info getPortInfo(int portIndex, int type) {
switch (type) {
case SRC_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), true);
case SRC_MICROPHONE: return Port.Info.MICROPHONE;
case SRC_LINE_IN: return Port.Info.LINE_IN;
case SRC_COMPACT_DISC: return Port.Info.COMPACT_DISC;
case DST_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), false);
case DST_SPEAKER: return Port.Info.SPEAKER;
case DST_HEADPHONE: return Port.Info.HEADPHONE;
case DST_LINE_OUT: return Port.Info.LINE_OUT;
}
// should never happen...
if (Printer.debug) Printer.debug("unknown port type: "+type);
return null;
}
/** Obtain the value of a default provider property.
@param typeClass The type of the default provider property. This
should be one of Receiver.class, Transmitter.class, Sequencer.class,
Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
Clip.class or Port.class.
@return The complete value of the property, if available.
If the property is not set, null is returned.
*/
private static synchronized String getDefaultProvider(Class typeClass) {
if (!SourceDataLine.class.equals(typeClass)
&& !TargetDataLine.class.equals(typeClass)
&& !Clip.class.equals(typeClass)
&& !Port.class.equals(typeClass)
&& !Receiver.class.equals(typeClass)
&& !Transmitter.class.equals(typeClass)
&& !Synthesizer.class.equals(typeClass)
&& !Sequencer.class.equals(typeClass)) {
return null;
}
String name = typeClass.getName();
String value = AccessController.doPrivileged(
(PrivilegedAction<String>) () -> System.getProperty(name));
if (value == null) {
value = getProperties().getProperty(name);
}
if ("".equals(value)) {
value = null;
}
return value;
}
private Port.Info getPortInfo(int portIndex, int type) {
switch (type) {
case SRC_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), true);
case SRC_MICROPHONE: return Port.Info.MICROPHONE;
case SRC_LINE_IN: return Port.Info.LINE_IN;
case SRC_COMPACT_DISC: return Port.Info.COMPACT_DISC;
case DST_UNKNOWN: return new PortInfo(nGetPortName(getID(), portIndex), false);
case DST_SPEAKER: return Port.Info.SPEAKER;
case DST_HEADPHONE: return Port.Info.HEADPHONE;
case DST_LINE_OUT: return Port.Info.LINE_OUT;
}
// should never happen...
if (Printer.debug) Printer.debug("unknown port type: "+type);
return null;
}
public Line getLine(Line.Info info) throws LineUnavailableException {
Line.Info fullInfo = getLineInfo(info);
if ((fullInfo != null) && (fullInfo instanceof Port.Info)) {
for (int i = 0; i < portInfos.length; i++) {
if (fullInfo.equals(portInfos[i])) {
return getPort(i);
}
}
}
throw new IllegalArgumentException("Line unsupported: " + info);
}
public int getMaxLines(Line.Info info) {
Line.Info fullInfo = getLineInfo(info);
// if it's not supported at all, return 0.
if (fullInfo == null) {
return 0;
}
if (fullInfo instanceof Port.Info) {
//return AudioSystem.NOT_SPECIFIED; // if several instances of PortMixerPort
return 1;
}
return 0;
}
Port getPort(int index) {
if (ports == null) {
ports = new PortMixerPort[portInfos.length];
}
if (ports[index] == null) {
ports[index] = new PortMixerPort((Port.Info)portInfos[index], this, index);
return ports[index];
}
// $$fb TODO: return (Port) (ports[index].clone());
return ports[index];
}
private PortMixerPort(Port.Info info,
PortMixer mixer,
int portIndex) {
super(info, mixer, null);
if (Printer.trace) Printer.trace("PortMixerPort CONSTRUCTOR: info: " + info);
this.portIndex = portIndex;
}
/**
* Starting a Thread that checks if the resources needed to the
* SpeechRecognition library are available
*/
protected void startResourcesThread() {
// alive?
if (resourcesThread != null && resourcesThread.isAlive())
return;
resourcesThread = new Thread(() -> {
try {
// Detect if the microphone is available
while (true) {
if (AudioSystem.isLineSupported(Port.Info.MICROPHONE)) {
// logger.log(Level.INFO, "Microphone is available.\n")
} else {
// logger.log(Level.INFO, "Microphone is not
// available.\n")
}
// Sleep some period
Thread.sleep(350);
}
} catch (InterruptedException ex) {
logger.log(Level.WARNING, null, ex);
resourcesThread.interrupt();
}
});
// Start
resourcesThread.start();
}
private PortMixerPort(Port.Info info,
PortMixer mixer,
int portIndex) {
super(info, mixer, null);
if (Printer.trace) Printer.trace("PortMixerPort CONSTRUCTOR: info: " + info);
this.portIndex = portIndex;
}
public Line getLine(Line.Info info) throws LineUnavailableException {
Line.Info fullInfo = getLineInfo(info);
if ((fullInfo != null) && (fullInfo instanceof Port.Info)) {
for (int i = 0; i < portInfos.length; i++) {
if (fullInfo.equals(portInfos[i])) {
return getPort(i);
}
}
}
throw new IllegalArgumentException("Line unsupported: " + info);
}
public int getMaxLines(Line.Info info) {
Line.Info fullInfo = getLineInfo(info);
// if it's not supported at all, return 0.
if (fullInfo == null) {
return 0;
}
if (fullInfo instanceof Port.Info) {
//return AudioSystem.NOT_SPECIFIED; // if several instances of PortMixerPort
return 1;
}
return 0;
}
/**
* Starting a Thread that checks if the resources needed to the
* SpeechRecognition library are available
*/
protected void startResourcesThread() {
// alive?
if (resourcesThread != null && resourcesThread.isAlive())
return;
resourcesThread = new Thread(() -> {
try {
// Detect if the microphone is available
while (true) {
if (AudioSystem.isLineSupported(Port.Info.MICROPHONE)) {
// logger.log(Level.INFO, "Microphone is available.\n")
} else {
// logger.log(Level.INFO, "Microphone is not
// available.\n")
}
// Sleep some period
Thread.sleep(350);
}
} catch (InterruptedException ex) {
logger.log(Level.WARNING, null, ex);
resourcesThread.interrupt();
}
});
// Start
resourcesThread.start();
}
Port getPort(int index) {
if (ports == null) {
ports = new PortMixerPort[portInfos.length];
}
if (ports[index] == null) {
ports[index] = new PortMixerPort((Port.Info)portInfos[index], this, index);
return ports[index];
}
// $$fb TODO: return (Port) (ports[index].clone());
return ports[index];
}
private PortMixerPort(Port.Info info,
PortMixer mixer,
int portIndex) {
super(info, mixer, null);
if (Printer.trace) Printer.trace("PortMixerPort CONSTRUCTOR: info: " + info);
this.portIndex = portIndex;
}
public Line getLine(Line.Info info) throws LineUnavailableException {
Line.Info fullInfo = getLineInfo(info);
if ((fullInfo != null) && (fullInfo instanceof Port.Info)) {
for (int i = 0; i < portInfos.length; i++) {
if (fullInfo.equals(portInfos[i])) {
return getPort(i);
}
}
}
throw new IllegalArgumentException("Line unsupported: " + info);
}
public int getMaxLines(Line.Info info) {
Line.Info fullInfo = getLineInfo(info);
// if it's not supported at all, return 0.
if (fullInfo == null) {
return 0;
}
if (fullInfo instanceof Port.Info) {
//return AudioSystem.NOT_SPECIFIED; // if several instances of PortMixerPort
return 1;
}
return 0;
}