我正在编写一个java程序来录制声音(使用Java Sound API),当我在插入耳机时运行我的代码时,程序正常工作,但是如果我在中间移除耳机,程序既没有捕获声音也不显示通知,表明未找到捕获设备。 我的目标是检测移除外部麦克风并自动切换到笔记本电脑的内置麦克风。
这是我的代码:
public void capAud() {
try {
af = getAudioFormate();
DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class,af);
td = (TargetDataLine)AudioSystem.getLine(dataLineInfo);
td.addLineListener(new MyListener());
new newthread().start();
} catch(Exception e) {
e.printStackTrace();
}
}
和“newThread”类
class newthread extends Thread {
public void run() {
baos=new ByteArrayOutputStream();
int len=32*1024;
try {
File file=new File("C:\\Users\\Programmer\\Desktop\\my.wav");
td.open(af);
td.start();
byte []temp=new byte[len];
while(td!=null) {
int num=td.read(temp, 0, len);
if(num>0) {
baos.write(temp, 0, num);
}
}
baos.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}