Skip to content

Commit

Permalink
Add AudioRecorder
Browse files Browse the repository at this point in the history
  • Loading branch information
Earth34r committed Dec 6, 2023
1 parent ac9470f commit 291a9db
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/main/java/AudioRecorder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import javax.sound.sampled.*;
import java.io.File;

public class AudioRecorder {

private static AudioFormat audioFormat = new AudioFormat(44100, 16, 2, true, true);
TargetDataLine targetDataLine;
boolean isRecording = true;

public void startRecording() {
Thread t = new Thread(
new Runnable(){
@Override
public void run(){
try {
// the format of the TargetDataLine
DataLine.Info dataLineInfo = new DataLine.Info(
TargetDataLine.class, audioFormat);
isRecording = true;
// the TargetDataLine used to capture audio data from the microphone
targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();

// the AudioInputStream that will be used to write the audio data to a file
AudioInputStream audioInputStream = new AudioInputStream(
targetDataLine);

// the file that will contain the audio data
File audioFile = new File("recording.wav");
AudioSystem.write(
audioInputStream,
AudioFileFormat.Type.WAVE,
audioFile);

} catch (Exception ex) {
ex.printStackTrace();
isRecording = false;
}
}
}
);
isRecording = true;
t.start();
}

public void stopRecording() {
targetDataLine.stop();
targetDataLine.close();
}

public boolean isRecording(){
return isRecording;
}


}

0 comments on commit 291a9db

Please sign in to comment.