Created
April 28, 2012 18:26
-
-
Save finnkuusisto/2521119 to your computer and use it in GitHub Desktop.
Test Java Sound Mixer Availability (for TinySound library)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.sound.sampled.AudioFormat; | |
import javax.sound.sampled.AudioSystem; | |
import javax.sound.sampled.DataLine; | |
import javax.sound.sampled.Line; | |
import javax.sound.sampled.LineUnavailableException; | |
import javax.sound.sampled.Mixer; | |
import javax.sound.sampled.SourceDataLine; | |
public class MixerTest { | |
public static final AudioFormat FORMAT = new AudioFormat( | |
AudioFormat.Encoding.PCM_SIGNED, //linear signed PCM | |
44100, //44.1kHz sampling rate | |
16, //16-bit | |
2, //2 channels fool | |
4, //frame size 4 bytes (16-bit, 2 channel) | |
44100, //same as sampling rate | |
false //little-endian | |
); | |
public static Mixer findMixer(AudioFormat format) { | |
DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class, | |
format); | |
Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo(); | |
//check each available mixer to see if it is acceptable | |
for (int i = 0; i < mixerInfos.length; i++) { | |
Mixer mixer = AudioSystem.getMixer(mixerInfos[i]); | |
//first check if it supports our line | |
if (!mixer.isLineSupported(lineInfo)) { | |
continue; //nope | |
} | |
//now check if we've used up our lines | |
int maxLines = mixer.getMaxLines(lineInfo); | |
//if it's not specified, it's supposedly unlimited | |
if (maxLines == AudioSystem.NOT_SPECIFIED) { | |
return mixer; | |
} | |
//otherwise we should count them | |
int linesOpen = 0; | |
Line[] sourceLines = mixer.getSourceLines(); | |
for (int s = 0; s < sourceLines.length; s++) { | |
//check if it matches our line | |
if (sourceLines[s].getLineInfo().matches(lineInfo)) { | |
linesOpen++; //one line used up | |
} | |
} | |
//now we can see if any are available | |
if (maxLines > linesOpen) { | |
return mixer; | |
} | |
} | |
//couldn't find one | |
return null; | |
} | |
public static void main(String[] args) { | |
DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class, | |
MixerTest.FORMAT); | |
Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo(); | |
System.out.println("----------"); | |
System.out.println("Audio Line"); | |
System.out.println("----------"); | |
System.out.println(lineInfo); | |
System.out.println("-----------------"); | |
System.out.println("Find Mixer Before"); | |
System.out.println("-----------------"); | |
System.out.println(findMixer(MixerTest.FORMAT).getMixerInfo()); | |
System.out.println("----------------"); | |
System.out.println("Mixers Available"); | |
System.out.println("----------------"); | |
for (int i = 0; i < mixerInfos.length; i++) { | |
System.out.println(mixerInfos[i]); | |
} | |
System.out.println("----------------------"); | |
System.out.println("Mixers Supporting Line"); | |
System.out.println("----------------------"); | |
for (int i = 0; i < mixerInfos.length; i++) { | |
Mixer m = AudioSystem.getMixer(mixerInfos[i]); | |
if (m.isLineSupported(lineInfo)) { | |
System.out.println(mixerInfos[i]); | |
} | |
} | |
System.out.println("-------------"); | |
System.out.println("Default Mixer"); | |
System.out.println("-------------"); | |
System.out.println(AudioSystem.getMixer(null).getMixerInfo()); | |
System.out.println("-----------------------------"); | |
System.out.println("AudioSystem.isLineSupported()"); | |
System.out.println("-----------------------------"); | |
System.out.println(AudioSystem.isLineSupported(lineInfo)); | |
SourceDataLine line = null; | |
try { | |
line = (SourceDataLine)AudioSystem.getLine(lineInfo); | |
line.open(MixerTest.FORMAT); | |
} catch (LineUnavailableException e) { } | |
System.out.println("-----------"); | |
System.out.println("Line Opened"); | |
System.out.println("-----------"); | |
System.out.println((line != null && line.isOpen())); | |
System.out.println("----------------"); | |
System.out.println("Find Mixer After"); | |
System.out.println("----------------"); | |
System.out.println(findMixer(MixerTest.FORMAT).getMixerInfo()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
effextus print lines? ------?