Using Java, I'm trying to record sound from the default microphone and show the current volume and mute status (as set at OS level, not interested in checking bytes if possible). So far, I can get the TargetDataLine and record to it using the following code:
TargetDataLine line = (TargetDataLine) AudioSystem.getLine(new DataLine.Info(TargetDataLine.class, formato));
This works great on Windows, line being the default microphone selected using the OS.
Right now, to get volume/mute controls I have the following code:
Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
for (Mixer.Info mixerInfo : mixerInfos) {
Mixer mixer = AudioSystem.getMixer(mixerInfo);
if (mixer.getMaxLines(Port.Info.MICROPHONE) > 0) {
System.out.println("-------------");
System.out.println(mixerInfo);
Port line = (Port) mixer.getLine(Port.Info.MICROPHONE);
line.open();
Line.Info[] targets = mixer.getTargetLineInfo();
Control[] controls = line.getControls();
for (Control control : controls) {
if (control instanceof CompoundControl) {
Control[] subControls = ((CompoundControl) control).getMemberControls();
for (Control subControl : subControls) {
System.out.println(subControl);
}
} else {
System.out.println(control);
}
}
System.out.println("-----------");
}
}
There are 2 microphones in my setup and I haven't found a way of knowing which is the default one (right now it is the second one in the array).
Tried Line port = AudioSystem.getLine(Port.Info.MICROPHONE);
and it is returning the first one available (and it is not the default in this instance).
So, any way to get the default microphone port that relates to the TargetDataLine obtained?
Thanks!
To get of all microphones installed in your system you can list them as follow :
package testing;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.TargetDataLine;
public class Main {
public static void main(String[] args) {
//Enumerates all available microphones
Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
for (Mixer.Info info: mixerInfos){
Mixer m = AudioSystem.getMixer(info);
Line.Info[] lineInfos = m.getTargetLineInfo();
if(lineInfos.length>=1 && lineInfos[0].getLineClass().equals(TargetDataLine.class)){//Only prints out info is it is a Microphone
System.out.println("Line Name: " + info.getName());//The name of the AudioDevice
System.out.println("Line Description: " + info.getDescription());//The type of audio device
for (Line.Info lineInfo:lineInfos){
System.out.println ("\t"+"---"+lineInfo);
Line line;
try {
line = m.getLine(lineInfo);
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
System.out.println("\t-----"+line);
}
}
}
}
}
Starting from that you can identify the default one... For instance in my MacBook the default one has this name "Built-in Microphone"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With