即使我已经在这里提出的许多问题中进行了搜索,我也没有找到解决此问题的示例(请不要发布其他链接,例如Java:Independently Capturing Audio From 2 Different Mic Inputs因为没有回答我可以试试)。
这是当前适用于 1 个麦克风的代码,但我找不到为第二个麦克风定义第二行的方法,请帮助 :)。
公共类 SecondTest {
public static void main(String[] args) throws LineUnavailableException {
ByteArrayOutputStream byteArrayOutputStream;
TargetDataLine line;
int counter1;
byte tempBuffer[] = new byte[8000];
int countzero, countdownTimer;
short convert[] = new short[tempBuffer.length];
/**
* This variable controls the accuracy of the volume recorded,
* for detecting lower volumes this number should be increased.
*/
int soundAccuarracy = 3000;
try {
byteArrayOutputStream = new ByteArrayOutputStream();
countdownTimer = 0;
//Start capturing
while (stopWatch.getTime() < timeOut) {
AudioFormat audioFormat = new AudioFormat(8000.0F, 16, 1, true, false);
DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
line = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
line.open(audioFormat);
line.start();
counter1 = line.read(tempBuffer, 0, tempBuffer.length);
byteArrayOutputStream.write(tempBuffer, 0, counter1);
try {
countzero = 0;
for (int i = 0; i < tempBuffer.length; i++) {
convert[i] = tempBuffer[i];
if (convert[i] == 0) {
countzero++;
}
}
countdownTimer++;
//This line prints the current volume accuracy detected and the times that the buffer run
System.out.println("Volume level is: " + countzero + ", counter number " + countdownTimer);
} catch (StringIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
}
Thread.sleep(0);
line.close();
}
} catch (Exception e) {
System.out.println(e);
}
}
}