我想制作一个写入文件的麦克风,但该文件为空。我不明白为什么。
public void recordSound () throws IOException, LineUnavailableException, InterruptedException {
// Assume that the TargetDataLine, line, has already
// been obtained and opened.
AudioFormat format = new AudioFormat(44100, 16, 2, true, true);
TargetDataLine line = AudioSystem.getTargetDataLine(format);
ByteArrayOutputStream out = new ByteArrayOutputStream();
int numBytesRead;
byte[] data = new byte[line.getBufferSize() / 5];
// Begin audio capture.
line.start();
// 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);
out.writeTo(new FileOutputStream("./audio.wav"));
Thread.sleep(1000);
line.stop();
line.close(); }
答案 0 :(得分:0)
你必须先打开这条线
line.open(line.getFormat());
line.start();