/
AudioRecorder.java
103 lines (63 loc) · 2.15 KB
/
AudioRecorder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package desktopscreenrecorder;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.TargetDataLine;
/**
*
* @author enyason
*/
public class AudioRecorder extends Thread {
private static TargetDataLine mic;
public void AudioRecorder() {
// initRecording();
}
private void initRecording() {
System.out.println("begin sound test...");
try {
//define audio format
AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
mic = (TargetDataLine) AudioSystem.getLine(info);
mic.open();
System.out.println("recording initialized...");
} catch (LineUnavailableException ex) {
ex.printStackTrace();
}
}
@Override
public void run() {
initRecording();
statRecording();
}
private void statRecording() {
try {
mic.start();
AudioInputStream audioInputStream = new AudioInputStream(mic);
File f = new File("audio_output.wav");
AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, f);
System.out.println("done writing to file");
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void stopRecording() {
mic.stop();
mic.close();
System.out.println("Recording ended");
}
}