-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |