My sound recording application works great in Mac OS X, but it doesn't work at all in Windows. I've tested it on two different Windows XP machines, as well as a virtual Windows XP install in Parallels on my Mac, but it's always the same thing: The TargetDataLine doesn't start after I open it. (This is on Java 1.6 throughout.)
I know it's not a hardware or OS issue because the
SimpleAudioRecorder code works fine on the Windows machines. And I'm using the exact same AudioFormat parameters that SimpleAudioRecorder uses, so that's not the issue either. I'm totally baffled.
I've reduced the program to just a few dozen lines (see below). On Mac OS X, it correctly prints "Running? true" but on Windows it prints "Running? false". Can anyone see what I'm doing wrong?
Thanks,
Trevor
import javax.sound.sampled.*;
public class SoundRecorder
{
public static void main(String[] args)
{
try
{
// String mixer = "Built-in Microphone"; // Use on Mac OS X
String mixer = "Primary Sound Capture Driver"; // Use on Windows
new SoundRecorder().open(mixer);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void open(String inputDeviceName) throws LineUnavailableException
{
Mixer mixer = getMixer(inputDeviceName);
if (mixer.getTargetLineInfo().length < 1)
{
throw new LineUnavailableException();
}
TargetDataLine targetDataLine = (TargetDataLine) mixer.getLine(mixer.getTargetLineInfo()[0]);
AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100.0F, 16, 2, 4, 44100.0F, false);
targetDataLine.open(audioFormat);
targetDataLine.start();
try
{
Thread.currentThread().sleep(2000);
}
catch (InterruptedException e) {}
System.out.println("Running? " + targetDataLine.isRunning());
}
private Mixer getMixer(String inputDeviceName) throws LineUnavailableException
{
for (Mixer.Info mixerInfo : AudioSystem.getMixerInfo())
{
if (mixerInfo.getName().equals(inputDeviceName))
{
return AudioSystem.getMixer(mixerInfo);
}
}
throw new LineUnavailableException("The mixer \"" + inputDeviceName + "\" is not installed on this system.");
}
}