JAVA监听音响话筒的代码
时间: 2023-05-12 13:03:11 浏览: 80
Java监听器
以下是JAVA监听音响话筒的代码:
import javax.sound.sampled.*;
public class MicListener {
public static void main(String[] args) {
AudioFormat format = new AudioFormat(44100, 16, 1, true, true);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line not supported");
System.exit(0);
}
try {
TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
line.start();
System.out.println("Listening...");
byte[] buffer = new byte[4096];
while (true) {
int bytesRead = line.read(buffer, 0, buffer.length);
for (int i = 0; i < bytesRead; i++) {
System.out.print(buffer[i] + " ");
}
}
} catch (LineUnavailableException ex) {
System.out.println("Line not available");
System.exit(0);
}
}
}
阅读全文