Skip to content

Instantly share code, notes, and snippets.

@Gliby
Last active October 11, 2015 14:05
Show Gist options
  • Save Gliby/04d05b4a5e5a3958f297 to your computer and use it in GitHub Desktop.
Save Gliby/04d05b4a5e5a3958f297 to your computer and use it in GitHub Desktop.
Input device manager for Java.
package net.gliby.voicechat.client.device;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.TargetDataLine;
public class Device {
private TargetDataLine line;
private Mixer.Info info;
public Device(TargetDataLine line, Mixer.Info info) {
this.line = line;
this.info = info;
}
public String getDescription() {
return info.getDescription();
}
public String getIdentifer() {
return info.getName();
}
public Mixer.Info getInfo() {
return info;
}
public TargetDataLine getLine() {
return line;
}
public String getName() {
return info.getName() != null ? info.getName() : "none";
}
public String getVendor() {
return info.getVendor();
}
public String getVersion() {
return info.getVersion();
}
public void setDevice(Device device) {
this.line = device.line;
this.info = device.info;
}
}
package net.gliby.voicechat.client.device;
import java.util.ArrayList;
import java.util.List;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.TargetDataLine;
import net.gliby.voicechat.client.sound.ClientStreamManager;
public class DeviceHandler {
private final List<Device> devices = new ArrayList<Device>();
public Device getDefaultDevice() {
TargetDataLine line;
final DataLine.Info info = new DataLine.Info(TargetDataLine.class, ClientStreamManager.getUniversalAudioFormat());
if (!AudioSystem.isLineSupported(info)) { return null; }
try {
line = (TargetDataLine) AudioSystem.getLine(info);
} catch (final Exception ex) {
return null;
}
if (line != null) return getDeviceByLine(line);
return null;
}
private Device getDeviceByLine(TargetDataLine line) {
for (int i = 0; i < devices.size(); i++) {
final Device device = devices.get(i);
if (device.getLine().getLineInfo().equals(line.getLineInfo())) { return device; }
}
return null;
}
public Device getDeviceByName(String deviceName) {
for (int i = 0; i < devices.size(); i++) {
final Device device = devices.get(i);
if (device.getName().equals(deviceName)) { return device; }
}
return null;
}
public List<Device> getDevices() {
return devices;
}
public boolean isEmpty() {
return devices.isEmpty();
}
public List<Device> loadDevices() {
devices.clear();
final Mixer.Info[] mixers = AudioSystem.getMixerInfo();
for (final Mixer.Info info : mixers) {
final Mixer mixer = AudioSystem.getMixer(info);
try {
final javax.sound.sampled.DataLine.Info tdlLineInfo = new DataLine.Info(TargetDataLine.class, ClientStreamManager.getUniversalAudioFormat());
final TargetDataLine tdl = (TargetDataLine) mixer.getLine(tdlLineInfo);
if (info != null) devices.add(new Device(tdl, info));
} catch (final LineUnavailableException e) {
} catch (final IllegalArgumentException e) {
}
}
return devices;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment