下面列出了怎么用javax.sound.sampled.Mixer的API类实例代码及写法,或者点击链接到github查看源代码。
public Mixer getMixer(Info info) {
if (!(info == null || info == SoftMixingMixer.info)) {
throw new IllegalArgumentException("Mixer " + info.toString()
+ " not supported by this provider.");
}
synchronized (mutex) {
if (lockthread != null)
if (Thread.currentThread() == lockthread)
throw new IllegalArgumentException("Mixer "
+ info.toString()
+ " not supported by this provider.");
if (globalmixer == null)
globalmixer = new SoftMixingMixer();
return globalmixer;
}
}
public static void main(String[] args) throws Exception {
Mixer.Info[] infos = AudioSystem.getMixerInfo();
log("" + infos.length + " mixers detected");
for (int i=0; i<infos.length; i++) {
Mixer mixer = AudioSystem.getMixer(infos[i]);
log("Mixer " + (i+1) + ": " + infos[i]);
try {
mixer.open();
for (Scenario scenario: scenarios) {
testSDL(mixer, scenario);
testTDL(mixer, scenario);
}
mixer.close();
} catch (LineUnavailableException ex) {
log("LineUnavailableException: " + ex);
}
}
if (failed == 0) {
log("PASSED (" + total + " tests)");
} else {
log("FAILED (" + failed + " of " + total + " tests)");
throw new Exception("Test FAILED");
}
}
public static void main(String[] args) throws Exception {
if (isSoundcardInstalled()) {
bais.mark(0);
run(null);
Mixer.Info[] infos = AudioSystem.getMixerInfo();
for (int i = 0; i<infos.length; i++) {
try {
Mixer m = AudioSystem.getMixer(infos[i]);
run(m);
} catch (Exception e) {
}
}
out("Waiting 1 second to dispose of all threads");
Thread.sleep(1000);
if (getClipThreadCount() > 0) {
out("Unused clip threads exist! Causes test failure");
failed = true;
}
if (failed) throw new Exception("Test FAILED!");
if (success > 0) {
out("Test passed.");
} else {
System.err.println("Test could not execute: please install an audio device");
}
}
}
/**
* Returns true if at least one soundcard is correctly installed
* on the system.
*/
public static boolean isSoundcardInstalled() {
boolean result = false;
try {
Mixer.Info[] mixers = AudioSystem.getMixerInfo();
if (mixers.length > 0) {
result = AudioSystem.getSourceDataLine(null) != null;
}
} catch (Exception e) {
System.err.println("Exception occured: "+e);
}
if (!result) {
System.err.println("Soundcard does not exist or sound drivers not installed!");
System.err.println("This test requires sound drivers for execution.");
}
return result;
}
/**
* Returns true if at least one soundcard is correctly installed
* on the system.
*/
public static boolean isSoundcardInstalled() {
boolean result = false;
try {
Mixer.Info[] mixers = AudioSystem.getMixerInfo();
if (mixers.length > 0) {
result = AudioSystem.getSourceDataLine(null) != null;
}
} catch (Exception e) {
System.err.println("Exception occured: " + e);
}
if (!result) {
System.err.println(
"Soundcard does not exist or sound drivers not installed!");
System.err.println(
"This test requires sound drivers for execution.");
}
return result;
}
public static void main(String[] args) throws Exception {
Mixer.Info[] infos = AudioSystem.getMixerInfo();
log("" + infos.length + " mixers detected");
for (int i=0; i<infos.length; i++) {
Mixer mixer = AudioSystem.getMixer(infos[i]);
log("Mixer " + (i+1) + ": " + infos[i]);
try {
mixer.open();
for (Scenario scenario: scenarios) {
testSDL(mixer, scenario);
testTDL(mixer, scenario);
}
mixer.close();
} catch (LineUnavailableException ex) {
log("LineUnavailableException: " + ex);
}
}
if (failed == 0) {
log("PASSED (" + total + " tests)");
} else {
log("FAILED (" + failed + " of " + total + " tests)");
throw new Exception("Test FAILED");
}
}
public Mixer getMixer(Info info) {
if (!(info == null || info == SoftMixingMixer.info)) {
throw new IllegalArgumentException("Mixer " + info.toString()
+ " not supported by this provider.");
}
synchronized (mutex) {
if (lockthread != null)
if (Thread.currentThread() == lockthread)
throw new IllegalArgumentException("Mixer "
+ info.toString()
+ " not supported by this provider.");
if (globalmixer == null)
globalmixer = new SoftMixingMixer();
return globalmixer;
}
}
/**
* Constructs a new AbstractMixer.
* @param mixerInfo the mixer with which this line is associated
* @param controls set of supported controls
*/
protected AbstractMixer(Mixer.Info mixerInfo,
Control[] controls,
Line.Info[] sourceLineInfo,
Line.Info[] targetLineInfo) {
// Line.Info, AbstractMixer, Control[]
super(new Line.Info(Mixer.class), null, controls);
// setup the line part
this.mixer = this;
if (controls == null) {
controls = new Control[0];
}
// setup the mixer part
this.mixerInfo = mixerInfo;
this.sourceLineInfo = sourceLineInfo;
this.targetLineInfo = targetLineInfo;
}
/**
* Returns true if at least one soundcard is correctly installed
* on the system.
*/
public static boolean isSoundcardInstalled() {
boolean result = false;
try {
Mixer.Info[] mixers = AudioSystem.getMixerInfo();
if (mixers.length > 0) {
result = AudioSystem.getSourceDataLine(null) != null;
}
} catch (Exception e) {
System.err.println("Exception occured: "+e);
}
if (!result) {
System.err.println("Soundcard does not exist or sound drivers not installed!");
System.err.println("This test requires sound drivers for execution.");
}
return result;
}
public Mixer getMixer(Info info) {
if (!(info == null || info == SoftMixingMixer.info)) {
throw new IllegalArgumentException("Mixer " + info.toString()
+ " not supported by this provider.");
}
synchronized (mutex) {
if (lockthread != null)
if (Thread.currentThread() == lockthread)
throw new IllegalArgumentException("Mixer "
+ info.toString()
+ " not supported by this provider.");
if (globalmixer == null)
globalmixer = new SoftMixingMixer();
return globalmixer;
}
}
public static void main(String[] args) throws Exception {
Mixer.Info[] infos = AudioSystem.getMixerInfo();
log("" + infos.length + " mixers detected");
for (int i=0; i<infos.length; i++) {
Mixer mixer = AudioSystem.getMixer(infos[i]);
log("Mixer " + (i+1) + ": " + infos[i]);
try {
mixer.open();
for (Scenario scenario: scenarios) {
testSDL(mixer, scenario);
testTDL(mixer, scenario);
}
mixer.close();
} catch (LineUnavailableException ex) {
log("LineUnavailableException: " + ex);
}
}
if (failed == 0) {
log("PASSED (" + total + " tests)");
} else {
log("FAILED (" + failed + " of " + total + " tests)");
throw new Exception("Test FAILED");
}
}
public Mixer getMixer(Mixer.Info info) {
synchronized (DirectAudioDeviceProvider.class) {
// if the default device is asked, we provide the mixer
// with SourceDataLine's
if (info == null) {
for (int i = 0; i < infos.length; i++) {
Mixer mixer = getDevice(infos[i]);
if (mixer.getSourceLineInfo().length > 0) {
return mixer;
}
}
}
// otherwise get the first mixer that matches
// the requested info object
for (int i = 0; i < infos.length; i++) {
if (infos[i].equals(info)) {
return getDevice(infos[i]);
}
}
}
throw new IllegalArgumentException("Mixer " + info.toString() + " not supported by this provider.");
}
/**
* Constructs a new AbstractMixer.
* @param mixer the mixer with which this line is associated
* @param controls set of supported controls
*/
protected AbstractMixer(Mixer.Info mixerInfo,
Control[] controls,
Line.Info[] sourceLineInfo,
Line.Info[] targetLineInfo) {
// Line.Info, AbstractMixer, Control[]
super(new Line.Info(Mixer.class), null, controls);
// setup the line part
this.mixer = this;
if (controls == null) {
controls = new Control[0];
}
// setup the mixer part
this.mixerInfo = mixerInfo;
this.sourceLineInfo = sourceLineInfo;
this.targetLineInfo = targetLineInfo;
}
public TGMixer() {
super(new Line.Info(Mixer.class));
this.lines = new HashMap<Class<?>, Line>();
this.sourceLineInfo = new ArrayList<DataLine.Info>();
this.targetLineInfo = new ArrayList<DataLine.Info>();
List<AudioFormat> formats = new ArrayList<AudioFormat>();
for (int channels = 1; channels <= 2; channels++) {
formats.add(new AudioFormat(Encoding.PCM_SIGNED, AudioSystem.NOT_SPECIFIED, 8, channels, channels, AudioSystem.NOT_SPECIFIED, false));
formats.add(new AudioFormat(Encoding.PCM_UNSIGNED, AudioSystem.NOT_SPECIFIED, 8, channels, channels, AudioSystem.NOT_SPECIFIED, false));
for (int bits = 16; bits < 32; bits += 8) {
formats.add(new AudioFormat(Encoding.PCM_SIGNED, AudioSystem.NOT_SPECIFIED, bits, channels, channels * bits / 8, AudioSystem.NOT_SPECIFIED, false));
formats.add(new AudioFormat(Encoding.PCM_UNSIGNED, AudioSystem.NOT_SPECIFIED, bits, channels, channels * bits / 8, AudioSystem.NOT_SPECIFIED, false));
formats.add(new AudioFormat(Encoding.PCM_SIGNED, AudioSystem.NOT_SPECIFIED, bits, channels, channels * bits / 8, AudioSystem.NOT_SPECIFIED, true));
formats.add(new AudioFormat(Encoding.PCM_UNSIGNED, AudioSystem.NOT_SPECIFIED, bits, channels, channels * bits / 8, AudioSystem.NOT_SPECIFIED, true));
}
formats.add(new AudioFormat(AudioFloatConverter.PCM_FLOAT, AudioSystem.NOT_SPECIFIED, 32, channels, channels * 4, AudioSystem.NOT_SPECIFIED, false));
formats.add(new AudioFormat(AudioFloatConverter.PCM_FLOAT, AudioSystem.NOT_SPECIFIED, 32, channels, channels * 4, AudioSystem.NOT_SPECIFIED, true));
formats.add(new AudioFormat(AudioFloatConverter.PCM_FLOAT, AudioSystem.NOT_SPECIFIED, 64, channels, channels * 8, AudioSystem.NOT_SPECIFIED, false));
formats.add(new AudioFormat(AudioFloatConverter.PCM_FLOAT, AudioSystem.NOT_SPECIFIED, 64, channels, channels * 8, AudioSystem.NOT_SPECIFIED, true));
}
this.sourceLineInfo.add(new DataLine.Info(SourceDataLine.class, formats.toArray(new AudioFormat[formats.size()]), AudioSystem.NOT_SPECIFIED, AudioSystem.NOT_SPECIFIED));
}
public Mixer getMixer(Mixer.Info info) {
synchronized (DirectAudioDeviceProvider.class) {
// if the default device is asked, we provide the mixer
// with SourceDataLine's
if (info == null) {
for (int i = 0; i < infos.length; i++) {
Mixer mixer = getDevice(infos[i]);
if (mixer.getSourceLineInfo().length > 0) {
return mixer;
}
}
}
// otherwise get the first mixer that matches
// the requested info object
for (int i = 0; i < infos.length; i++) {
if (infos[i].equals(info)) {
return getDevice(infos[i]);
}
}
}
throw new IllegalArgumentException("Mixer " + info.toString() + " not supported by this provider.");
}
/**
* Constructs a new AbstractMixer.
* @param mixer the mixer with which this line is associated
* @param controls set of supported controls
*/
protected AbstractMixer(Mixer.Info mixerInfo,
Control[] controls,
Line.Info[] sourceLineInfo,
Line.Info[] targetLineInfo) {
// Line.Info, AbstractMixer, Control[]
super(new Line.Info(Mixer.class), null, controls);
// setup the line part
this.mixer = this;
if (controls == null) {
controls = new Control[0];
}
// setup the mixer part
this.mixerInfo = mixerInfo;
this.sourceLineInfo = sourceLineInfo;
this.targetLineInfo = targetLineInfo;
}
public static void main(String[] args) throws Exception {
Mixer.Info[] infos = AudioSystem.getMixerInfo();
log("" + infos.length + " mixers detected");
for (int i=0; i<infos.length; i++) {
Mixer mixer = AudioSystem.getMixer(infos[i]);
log("Mixer " + (i+1) + ": " + infos[i]);
try {
mixer.open();
for (Scenario scenario: scenarios) {
testSDL(mixer, scenario);
testTDL(mixer, scenario);
}
mixer.close();
} catch (LineUnavailableException ex) {
log("LineUnavailableException: " + ex);
}
}
if (failed == 0) {
log("PASSED (" + total + " tests)");
} else {
log("FAILED (" + failed + " of " + total + " tests)");
throw new Exception("Test FAILED");
}
}
/**
* Returns true if at least one soundcard is correctly installed
* on the system.
*/
public static boolean isSoundcardInstalled() {
boolean result = false;
try {
Mixer.Info[] mixers = AudioSystem.getMixerInfo();
if (mixers.length > 0) {
result = AudioSystem.getSourceDataLine(null) != null;
}
} catch (Exception e) {
System.err.println("Exception occured: "+e);
}
if (!result) {
System.err.println("Soundcard does not exist or sound drivers not installed!");
System.err.println("This test requires sound drivers for execution.");
}
return result;
}
public static void main(String[] args) throws Exception {
Mixer.Info[] infos = AudioSystem.getMixerInfo();
log("" + infos.length + " mixers detected");
for (int i=0; i<infos.length; i++) {
Mixer mixer = AudioSystem.getMixer(infos[i]);
log("Mixer " + (i+1) + ": " + infos[i]);
try {
mixer.open();
for (Scenario scenario: scenarios) {
testSDL(mixer, scenario);
testTDL(mixer, scenario);
}
mixer.close();
} catch (LineUnavailableException ex) {
log("LineUnavailableException: " + ex);
}
}
if (failed == 0) {
log("PASSED (" + total + " tests)");
} else {
log("FAILED (" + failed + " of " + total + " tests)");
throw new Exception("Test FAILED");
}
}
public Mixer getMixer(Mixer.Info info) {
synchronized (DirectAudioDeviceProvider.class) {
// if the default device is asked, we provide the mixer
// with SourceDataLine's
if (info == null) {
for (int i = 0; i < infos.length; i++) {
Mixer mixer = getDevice(infos[i]);
if (mixer.getSourceLineInfo().length > 0) {
return mixer;
}
}
}
// otherwise get the first mixer that matches
// the requested info object
for (int i = 0; i < infos.length; i++) {
if (infos[i].equals(info)) {
return getDevice(infos[i]);
}
}
}
throw new IllegalArgumentException("Mixer " + info.toString() + " not supported by this provider.");
}
/**
* Constructs a new AbstractMixer.
* @param mixerInfo the mixer with which this line is associated
* @param controls set of supported controls
*/
protected AbstractMixer(Mixer.Info mixerInfo,
Control[] controls,
Line.Info[] sourceLineInfo,
Line.Info[] targetLineInfo) {
// Line.Info, AbstractMixer, Control[]
super(new Line.Info(Mixer.class), null, controls);
// setup the line part
this.mixer = this;
if (controls == null) {
controls = new Control[0];
}
// setup the mixer part
this.mixerInfo = mixerInfo;
this.sourceLineInfo = sourceLineInfo;
this.targetLineInfo = targetLineInfo;
}
public Mixer getMixer(Info info) {
if (!(info == null || info == SoftMixingMixer.info)) {
throw new IllegalArgumentException("Mixer " + info.toString()
+ " not supported by this provider.");
}
synchronized (mutex) {
if (lockthread != null)
if (Thread.currentThread() == lockthread)
throw new IllegalArgumentException("Mixer "
+ info.toString()
+ " not supported by this provider.");
if (globalmixer == null)
globalmixer = new SoftMixingMixer();
return globalmixer;
}
}
public Mixer getMixer(Mixer.Info info) {
synchronized (DirectAudioDeviceProvider.class) {
// if the default device is asked, we provide the mixer
// with SourceDataLine's
if (info == null) {
for (int i = 0; i < infos.length; i++) {
Mixer mixer = getDevice(infos[i]);
if (mixer.getSourceLineInfo().length > 0) {
return mixer;
}
}
}
// otherwise get the first mixer that matches
// the requested info object
for (int i = 0; i < infos.length; i++) {
if (infos[i].equals(info)) {
return getDevice(infos[i]);
}
}
}
throw new IllegalArgumentException("Mixer " + info.toString() + " not supported by this provider.");
}
private static Mixer getDevice(DirectAudioDeviceInfo info) {
int index = info.getIndex();
if (devices[index] == null) {
devices[index] = new DirectAudioDevice(info);
}
return devices[index];
}
public Mixer.Info[] getMixerInfo() {
synchronized (PortMixerProvider.class) {
Mixer.Info[] localArray = new Mixer.Info[infos.length];
System.arraycopy(infos, 0, localArray, 0, infos.length);
return localArray;
}
}
public void setDevice(int position) throws LineUnavailableException, IllegalArgumentException {
if (position == 0 || position == -1) {
initializeOutput();
} else {
Mixer appMixer = mixerList[position];
Info sdlLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
sourceDataLine = (SourceDataLine) appMixer.getLine(sdlLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
}
}
public static void main(String[] args) throws Exception {
out("4936397: Verify that there'll be either SimpleInputDevice OR DirectAudioDevice");
boolean foundSimpleInputDevice = false;
boolean foundDirectAudioDevice = false;
Mixer.Info[] aInfos = AudioSystem.getMixerInfo();
for (int i = 0; i < aInfos.length; i++) {
try {
Mixer mixer = AudioSystem.getMixer(aInfos[i]);
String mixerClass = mixer.getClass().toString();
if (mixerClass.indexOf("SimpleInputDevice") >= 0) {
out("Found SimpleInputDevice: "+aInfos[i]);
foundSimpleInputDevice = true;
}
if (mixerClass.indexOf("DirectAudioDevice") >= 0) {
out("Found DirectAudioDevice: "+aInfos[i]);
foundDirectAudioDevice = true;
}
} catch (Exception e) {
out("Unexpected exception: "+e);
}
}
if (aInfos.length == 0) {
out("[No mixers available] - cannot exercise this test.");
} else {
if (foundSimpleInputDevice && foundDirectAudioDevice) {
out("Found both types of capture devices!");
throw new Exception("Test FAILED!");
}
out("Did not find both types of capture devices. Test passed");
}
}
public Mixer.Info[] getMixerInfo() {
synchronized (DirectAudioDeviceProvider.class) {
Mixer.Info[] localArray = new Mixer.Info[infos.length];
System.arraycopy(infos, 0, localArray, 0, infos.length);
return localArray;
}
}
private static Mixer getDevice(DirectAudioDeviceInfo info) {
int index = info.getIndex();
if (devices[index] == null) {
devices[index] = new DirectAudioDevice(info);
}
return devices[index];
}
private static Mixer getDevice(PortMixerInfo info) {
int index = info.getIndex();
if (devices[index] == null) {
devices[index] = new PortMixer(info);
}
return devices[index];
}