Dear All,
I'm learning Java Sound API to capture and playback audios. It worked fine with built-in sound card of laptop, which is a "Realtek". In addition, I have a USB headset with audio adapter, which is a "Plantronics". But it didn't work too well with Java Sound, following are the details.
1) System Environment
1.1) OS: Windows 7 Professional 64 bits
1.2) Java Version: 1.7.0_55 64 bits
1.3) Audio devices: "Realtek" and "Plantronics", both are detected as Direct Audio Device by AudioSystem of Java Sound
2) Expected result:
2.1) Plug-in the USB headset with audio adapter, able to get the Mixer.Info of the headset from AudioSystem within few seconds
2.2) Plug-out the USB headset with audio adapter, able to detect the Mixer.Info of headset have been removed from AudioSystem within few seconds
3) Actual result:
3.1) Plug-in the USB headset with audio adapter, able to get the Mixer.Info of the headset from AudioSystem after more than 1 minute
3.2) Plug-out the USB headset with audio adapter, able to detect the Mixer.Info of headset have been removed from AudioSystem after more than 1 minute
4) Implementation:
public static Mixer getMixer(final String mixerName,
final String mixerDesc) {
Mixer mixer = null;
if (mixerName != null) {
Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
logger.trace("Mixer infos list size[" + mixerInfos.length + "]");
for (int i = 0; i < mixerInfos.length; i++) {
if (mixerInfos[i].getName().contains(mixerName)
&& (mixerDesc == null || mixerDesc.length() == 0
|| mixerInfos[i].getDescription().contains(mixerDesc))) {
mixer = AudioSystem.getMixer(mixerInfos[i]);
break;
}
}
}
return mixer;
}
Using the name of audio device "mixerName" instead of index is due to the index of audio device will dynamically changed when plug-in the USB headset. The description of audio device "mixerDesc" is used to differentiate the Capture and Playback type for Microphone and Speakers
My personal understanding is there seem like has a cache in the lower level of AudioSystem, which preserve the Mixer.Info[] for while. Please kindly point out if I infer wrongly. If I infer correctly, how do I eliminate the cache. If I infer wrongly, what are the other possibilities will lead the delay ?
Thank you for your kindly sharing and time.
Paul