下面列出了javax.sound.sampled.AudioSystem#getMixer ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void runVolumeCommand(Closure closure) {
Mixer.Info[] infos = AudioSystem.getMixerInfo();
for (Mixer.Info info : infos) {
Mixer mixer = AudioSystem.getMixer(info);
if (mixer.isLineSupported(Port.Info.SPEAKER)) {
Port port;
try {
port = (Port) mixer.getLine(Port.Info.SPEAKER);
port.open();
if (port.isControlSupported(FloatControl.Type.VOLUME)) {
FloatControl volume = (FloatControl) port.getControl(FloatControl.Type.VOLUME);
closure.execute(volume);
}
port.close();
} catch (LineUnavailableException e) {
logger.error("Cannot access master volume control", e);
}
}
}
}
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 void setGain(float ctrl) {
try {
Mixer.Info[] infos = AudioSystem.getMixerInfo();
for (Mixer.Info info : infos) {
Mixer mixer = AudioSystem.getMixer(info);
if (mixer.isLineSupported(Port.Info.SPEAKER)) {
try ( Port port = (Port) mixer.getLine(Port.Info.SPEAKER)) {
port.open();
if (port.isControlSupported(FloatControl.Type.VOLUME)) {
FloatControl volume = (FloatControl) port.getControl(FloatControl.Type.VOLUME);
volume.setValue(ctrl);
}
}
}
}
} catch (Exception e) {
}
}
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 {
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 {
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");
}
}
private static boolean testMixers(Mixer.Info[] infos,
String providerClassName) {
boolean allOk = true;
for (int i = 0; i < infos.length; i++) {
Mixer mixer = null;
try {
mixer = AudioSystem.getMixer(infos[i]);
} catch (NullPointerException e) {
out("Exception thrown; Test NOT failed.");
e.printStackTrace();
}
for (int j = 0; j < lineClasses.length; j++) {
if (mixer.isLineSupported(new Line.Info(lineClasses[j]))) {
allOk &= testMixer(mixer, lineClasses[j],
providerClassName);
}
}
}
return allOk;
}
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");
}
}
/**
* Build device info and determine default devices.
*/
private void sniffAvailableMixers() {
Mixer.Info[] mixers = AudioSystem.getMixerInfo();
for (int i = 0; i < mixers.length; i++) {
DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.name = mixers[i].getName();
Mixer mixer = AudioSystem.getMixer(mixers[i]);
Line.Info[] lines = mixer.getTargetLineInfo();
deviceInfo.maxInputs = scanMaxChannels(lines);
// Remember first device that supports input.
if ((defaultInputDeviceID < 0) && (deviceInfo.maxInputs > 0)) {
defaultInputDeviceID = i;
}
lines = mixer.getSourceLineInfo();
deviceInfo.maxOutputs = scanMaxChannels(lines);
// Remember first device that supports output.
if ((defaultOutputDeviceID < 0) && (deviceInfo.maxOutputs > 0)) {
defaultOutputDeviceID = i;
}
deviceRecords.add(deviceInfo);
}
}
public static void main(String[] args) throws Exception {
out("4936397: Verify that there'll for a given endianness, there's also the little endian version");
out(" and the same for signed'ness for 8-bit formats");
Mixer.Info[] aInfos = AudioSystem.getMixerInfo();
for (int i = 0; i < aInfos.length; i++) {
try {
Mixer mixer = AudioSystem.getMixer(aInfos[i]);
out("Mixer "+aInfos[i]);
checkLines(mixer, mixer.getSourceLineInfo());
checkLines(mixer, mixer.getTargetLineInfo());
} catch (Exception e) {
out("Unexpected exception when getting a mixer: "+e);
}
}
if (testedFormats == 0) {
out("[No appropriate lines available] - cannot exercise this test.");
} else {
if (failed) {
throw new Exception("Test FAILED!");
}
out("Test passed");
}
}
protected void printAllMixerNames() {
for(Mixer.Info info : AudioSystem.getMixerInfo()) {
P.out(info.getName(), " - ", info.getDescription());
Mixer m = AudioSystem.getMixer(info);
mixers.add(m);
UI.addButton(info.getName(), false);
}
}
public static Mixer tryToFindMixer(AudioFormat audioFormat)
{
Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
Mixer[] mixers = new Mixer[mixerInfos.length];
final DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
if(mixers.length == 0) {
return null;
}
for(int i=0; i<mixerInfos.length; ++i) {
mixers[i] = AudioSystem.getMixer(mixerInfos[i]);
}
Arrays.sort(mixers, new Comparator<Mixer>()
{
@Override
public int compare(Mixer mixer1, Mixer mixer2)
{
int numLines1 = mixer1.getMaxLines(dataLineInfo);
int numLines2 = mixer2.getMaxLines(dataLineInfo);
if(numLines1 == AudioSystem.NOT_SPECIFIED || numLines1 > numLines2) {
return -1;
}
return 1;
}
});
if(mixers[0].getMaxLines(dataLineInfo) == 0) {
return null;
}
return mixers[0];
}
/**
* 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;
}
public Mixer getMixer(final String name) {
Mixer mixer = null;
if (name != null) {
final Mixer.Info[] mInfos = AudioSystem.getMixerInfo();
if (mInfos != null) {
for (final Info mInfo : mInfos) {
if (mInfo.getName().equals(name)) {
mixer = AudioSystem.getMixer(mInfo);
break;
}
}
}
}
return mixer;
}
public static void main(String[] args) throws Exception {
if (isSoundcardInstalled()) {
int res=0;
int count = 0;
Mixer.Info[] infos = AudioSystem.getMixerInfo();
for (int i = -1; i<infos.length; i++) {
try {
Mixer m;
if (i == -1) {
m = null;
} else {
m = AudioSystem.getMixer(infos[i]);
}
int r = run(m, AudioSystem.NOT_SPECIFIED);
// only continue if successful
if (r == 0) {
count++;
r = run(m, -2);
if (r == 1) {
// only fail if IAE was thrown
System.out.println("#FAILED: using -2 for buffer size does not work!");
res = 1;
}
}
} catch (Exception e) {
}
}
if (res!=1) {
System.out.println("Test passed");
} else {
if (count == 0) {
System.err.println("Test could not execute -- no suitable mixers installed. NOT failed");
}
throw new Exception("Test FAILED!");
}
}
}
public static void main(String[] args) throws Exception {
boolean res=true;
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
System.out.println(mixerInfo.length+" mixers on system.");
if (mixerInfo.length == 0) {
System.out.println("Cannot execute test. Not Failed!");
} else {
for (int i = 0; i < mixerInfo.length; i++) {
Mixer mixer = AudioSystem.getMixer(mixerInfo[i]);
System.out.println();
System.out.println(mixer+":");
showMixerLines(mixer.getSourceLineInfo());
showMixerLines(mixer.getTargetLineInfo());
}
res=ok16 && ok32;
}
if (res) {
System.out.println("Test passed");
} else {
System.out.println("Test failed");
throw new Exception("Test failed");
}
//ystem.exit(res?0:1);
}
public static void main(String[] args) throws Exception {
boolean res=true;
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
for (int i = 0; i < mixerInfo.length; i++) {
Mixer mixer = AudioSystem.getMixer(mixerInfo[i]);
System.out.println(mixer);
Line.Info[] lineinfo = mixer.getSourceLineInfo();
for (int j = 0; j < lineinfo.length; j++) {
System.out.println(" " + lineinfo[j]);
try {
AudioFormat[] formats = ((DataLine.Info)lineinfo[j]).getFormats();
for (int k = 0; k < formats.length; k++) {
if ( (formats[k].getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)
|| formats[k].getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED))
&& (formats[k].getFrameSize() != AudioSystem.NOT_SPECIFIED)
&& ((formats[k].getSampleSizeInBits() == 16) || (formats[k].getSampleSizeInBits() == 8))
&& ((((formats[k].getSampleSizeInBits() + 7) / 8) * formats[k].getChannels()) != formats[k].getFrameSize())) {
System.out.println(" # " + formats[k] + ", getFrameSize() wrongly returns"+ formats[k].getFrameSize());
res=false;
}
}
} catch (ClassCastException e) {
}
}
}
if (res) {
System.out.println("Test passed");
} else {
System.out.println("Test failed");
throw new Exception("Test failed");
}
}
public static void main(String argv[]) throws Exception {
boolean success = true;
Mixer.Info [] infos = AudioSystem.getMixerInfo();
for (int i=0; i<infos.length; i++) {
Mixer mixer = AudioSystem.getMixer(infos[i]);
System.out.println("Mixer is: " + mixer);
Line.Info [] target_line_infos = mixer.getTargetLineInfo();
for (int j = 0; j < target_line_infos.length; j++) {
try {
System.out.println("Trying to get:" + target_line_infos[j]);
mixer.getLine(target_line_infos[j]);
} catch (IllegalArgumentException iae) {
System.out.println("Unexpected IllegalArgumentException raised:");
iae.printStackTrace();
success = false;
} catch (LineUnavailableException lue) {
System.out.println("Unexpected LineUnavailableException raised:");
lue.printStackTrace();
success = false;
}
}
}
if (success) {
System.out.println("Test passed");
} else {
throw new Exception("Test FAILED");
}
}
public static void main(String[] args) throws Exception {
println("This is an interactive test for DirectAudio.");
println("If the tone repeats, the test is failed.");
println("");
println("Make sure that you have speakers connected");
println("and that the system mixer is not muted.");
println("");
println("Press a key to start the test.");
key();
Mixer.Info[] mixers=null;
println(" ...using self-generated sine wave for playback");
audioFormat = new AudioFormat((float)sampleRate, 8, 1, true, true);
for (int i=0; i<audioData.length; i++) {
audioData[i] = (byte)(Math.sin(RAD*frequency/sampleRate*i)*127.0);
}
info = new DataLine.Info(SourceDataLine.class, audioFormat);
mixers = AudioSystem.getMixerInfo();
int succMixers = 0;
for (int i=0; i<mixers.length; i++) {
println(""+mixers[i]+":");
if ((mixers[i].getName()+mixers[i].getDescription()+mixers[i].getVendor()).indexOf("Direct") < 0) {
println(" ->not a DirectAudio Mixer!");
} else {
try {
Mixer mixer = AudioSystem.getMixer(mixers[i]);
if (!mixer.isLineSupported(info)) {
println(" ->doesn't support SourceDataLine!");
} else {
succMixers++;
println(" -> is getting tested.");
play(mixer);
}
} catch (Exception e) {
println(" -> Exception occured: "+e);
e.printStackTrace();
}
}
}
if (succMixers == 0) {
println("No DirectAudio mixers available! ");
println("Cannot run test.");
}
}
public static void printAudioInfo() {
P.out("----------------- printAudioInfo -------------------");
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
for(int i = 0; i < mixerInfo.length; i++) {
P.out("########## mixerInfo["+i+"]", mixerInfo[i].getName());
// Mixer mixer = AudioSystem.getMixer(null); // default mixer
Mixer mixer = AudioSystem.getMixer(mixerInfo[i]); // default mixer
try {
mixer.open();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
P.out("Supported SourceDataLines of default mixer (%s):\n\n", mixer.getMixerInfo().getName());
for(Line.Info info : mixer.getSourceLineInfo()) {
if(SourceDataLine.class.isAssignableFrom(info.getLineClass())) {
SourceDataLine.Info info2 = (SourceDataLine.Info) info;
P.out(info2);
System.out.printf(" max buffer size: \t%d\n", info2.getMaxBufferSize());
System.out.printf(" min buffer size: \t%d\n", info2.getMinBufferSize());
AudioFormat[] formats = info2.getFormats();
P.out(" Supported Audio formats: ");
for(AudioFormat format : formats) {
P.out(" "+format);
System.out.printf(" encoding: %s\n", format.getEncoding());
System.out.printf(" channels: %d\n", format.getChannels());
System.out.printf(format.getFrameRate()==-1?"":" frame rate [1/s]: %s\n", format.getFrameRate());
System.out.printf(" frame size [bytes]: %d\n", format.getFrameSize());
System.out.printf(format.getSampleRate()==-1?"":" sample rate [1/s]: %s\n", format.getSampleRate());
System.out.printf(" sample size [bit]: %d\n", format.getSampleSizeInBits());
System.out.printf(" big endian: %b\n", format.isBigEndian());
Map<String,Object> prop = format.properties();
if(!prop.isEmpty()) {
P.out(" Properties: ");
for(Map.Entry<String, Object> entry : prop.entrySet()) {
System.out.printf(" %s: \t%s\n", entry.getKey(), entry.getValue());
}
}
}
P.out();
} else {
P.out(info.toString());
}
P.out();
}
mixer.close();
}
}