即使我在这里问了很多问题也很难找,我没有找到解决这个问题的例子(请不要发布其他类似的链接Java: Independently Capturing Audio From 2 Different Mic Inputs因为没有答案我可以尝试)。
以下是目前适用于1个麦克风的代码,但我找不到为第二个麦克风定义第二行的方法,请帮助:)。
public class 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);
}
}
}