我正试图编写一个小程序,当用户说话时会有反应。就像有一个圆圈变大或类似的东西。从麦克风记录的声音的音量
即时通讯使用此代码访问麦克风,但我如何才能使其反应只有当用户说话?例如当录制的音量大于某个数量时。
TargetDataLine line = null;
AudioFormat format = new AudioFormat(16000, 16, 1, true, true);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if(! AudioSystem.isLineSupported(info)){
System.out.println("Line is not supported");
}
try{
line = (TargetDataLine) AudioSystem.getLine(info);
line.open();
}catch(LineUnavailableException e){
System.out.println("Failed to get line");
System.exit(-1);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
int numBytesRead;
byte[] data = new byte[line.getBufferSize()/5];
// Begin audio capture.
line.start();
int i = 0;
// Here, stopped is a global boolean set by another thread.
while (i<100) {
// Read the next chunk of data from the TargetDataLine.
numBytesRead = line.read(data, 0, data.length);
// Save this chunk of data.
out.write(data, 0, numBytesRead);
i++;
System.out.println(i);
}
可能出现[录制时检测到静音]的副本(http://stackoverflow.com/questions/5800649/detect-silence-when-recording) –