-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaRecorder.java
More file actions
154 lines (128 loc) · 3.78 KB
/
JavaRecorder.java
File metadata and controls
154 lines (128 loc) · 3.78 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import java.awt.FileDialog;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
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.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
import javax.swing.filechooser.FileSystemView;
/*
* a simple java Recorder
*/
public class JavaRecorder {
public static AudioFormat getAudioFormat() {
AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
float rate = 8000f;
int sampleSize = 16;
boolean bigEndian = false;
int channels = 1;
return new AudioFormat(encoding, rate, sampleSize, channels, (sampleSize / 8) * channels, rate, bigEndian);
}
AudioFormat af;
boolean flg;
ByteArrayOutputStream baos = null;
DataLine.Info targetinfo,sourceinfo;
TargetDataLine td;
SourceDataLine sd;
JavaRecorder(){
try {
af = getAudioFormat();
targetinfo = new DataLine.Info(TargetDataLine.class, af);
td = (TargetDataLine) (AudioSystem.getLine(targetinfo));
sourceinfo = new DataLine.Info(SourceDataLine.class, af);
sd = (SourceDataLine) (AudioSystem.getLine(sourceinfo));
} catch (Exception e) {
e.printStackTrace();
}
}
void paly(String fileName){
try{
File file = new File(fileName);
AudioInputStream ais = AudioSystem.getAudioInputStream(file);
AudioFormat af = AudioSystem.getAudioFileFormat(file).getFormat();
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, af);
SourceDataLine sd = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sd.open();
sd.start();
byte buf[] = new byte[0xFF];
int len;
while ((len = ais.read(buf, 0, buf.length)) != -1) {
sd.write(buf, 0, len);
}
sd.drain();
sd.close();
ais.close();
} catch (Exception e){
e.printStackTrace();
}
}
class Recorder implements Runnable {
@Override
public void run() {
try {
System.out.println("Begin record");
td.open(af);
td.start();
byte bts[] = new byte[10000];
baos = new ByteArrayOutputStream();
flg = true;
while (flg) {
int cnt = td.read(bts, 0, bts.length);
if (cnt > 0) {
baos.write(bts, 0, cnt);
}
}
td.drain();
td.close();
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class RecordPlayer implements Runnable {
public void run() {
System.out.println("Begin playRecord");
try {
byte audioData[] = baos.toByteArray();
sd.open(af);
sd.start();
sd.write(audioData, 0, audioData.length);
sd.drain();
sd.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
void start(){
Thread t = new Thread(new Recorder());
t.start();
}
void stop(){
flg = false;
}
void playRecord(){
Thread t = new Thread(new RecordPlayer());
t.start();
}
void saveToDesktop() {
AudioFormat af = getAudioFormat();
byte audioData[] = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(audioData);
AudioInputStream ais = new AudioInputStream(bais, af, audioData.length / af.getFrameSize());
try {
File filePath = new File(FileSystemView.getFileSystemView().getHomeDirectory().getAbsolutePath());
File file = new File(filePath.getPath() + "/" + System.currentTimeMillis() + ".mp3");
AudioSystem.write(ais, AudioFileFormat.Type.WAVE, file);
} catch (Exception e) {
e.printStackTrace();
}
}
}