2016-12-09 13 views
-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(); } 
+1

最初の行を開く必要があります。それとも、Javaがあなたのために行うようにしてください(https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)。 – VGR

+0

私はそれをしました。うまくいきません –

+1

あなたのコードには他の問題があります。 'read'コールがすべてあなたのオーディオデータを取得することはほとんどありません。 – VGR

答えて

0

はあなたのFileOutputStreamを閉じ

line.open(line.getFormat()); 
line.start();