Reputation: 185
According to my research, Java's sound api does not play well with OsX. It has a hard time determining the active input, so it generally defaults to the first system input.
My solution is to iterate through an array of input ports, recording a couple milliseconds of audio and comparing those pieces. Whichever one has the greatest amplitude, I'll use as my input.
My question is, what would the best method be for generating an array of all input ports available to Java?
Upvotes: 1
Views: 4912
Reputation: 41
You can list all of the available Mixer objects using the following:
Mixer.Info[] mixers = AudioSystem.getMixerInfo();
for (Mixer.Info mixerInfo : mixers){
System.out.println(mixerInfo);
}
On my system, a Mac, this is the result:
Java Sound Audio Engine, version 1.0
Built-in Input, version Unknown Version
Built-in Microphone, version Unknown Version
Edit
Here's how to extract a list of valid Target lines that you can get audio input from:
Mixer.Info[] mixers = AudioSystem.getMixerInfo();
List<Line.Info> availableLines = new ArrayList<Line.Info>();
for (Mixer.Info mixerInfo : mixers){
System.out.println("Found Mixer: " + mixerInfo);
Mixer m = AudioSystem.getMixer(mixerInfo);
Line.Info[] lines = m.getTargetLineInfo();
for (Line.Info li : lines){
System.out.println("Found target line: " + li);
try {
m.open();
availableLines.add(li);
} catch (LineUnavailableException e){
System.out.println("Line unavailable.");
}
}
}
System.out.println("Available lines: " + availableLines);
Once you have the Line.Info object, you can get the TargetDataLine associated with the Line.Info object by calling AudioSystem.getLine() and using that Line.Info as a parameter.
Upvotes: 4
Reputation: 7910
Basics on how to determine what resources are available can be found here: Accessing Audio System Resources
I found this section to be the most helpful in terms of example code, in the Java Sound tutorials: http://download.oracle.com/javase/tutorial/sound/converters.html
Upvotes: 0