-7
ファイルに書き込むマイクを作成したいが、そのファイルは空です。なぜか分からない。マイクがうまくいかない理由
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(); }
最初の行を開く必要があります。それとも、Javaがあなたのために行うようにしてください(https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)。 – VGR
私はそれをしました。うまくいきません –
あなたのコードには他の問題があります。 'read'コールがすべてあなたのオーディオデータを取得することはほとんどありません。 – VGR