@@ -18,7 +18,7 @@ public class Microphone {
18
18
/**
19
19
* Enum for current Microphone state
20
20
*/
21
- private enum CaptureState {
21
+ public enum CaptureState {
22
22
PROCESSING_AUDIO , STARTING_CAPTURE , CLOSED
23
23
}
24
24
@@ -44,18 +44,8 @@ private enum CaptureState {
44
44
* STARTING_CAPTURE is returned if the Thread is setting variables<br>
45
45
* CLOSED is returned if the Thread is not doing anything/not capturing audio
46
46
*/
47
- public String getState () {
48
- switch (state ) {
49
- case PROCESSING_AUDIO :
50
- return "PROCESSING_AUDIO" ;
51
- case STARTING_CAPTURE :
52
- return "STARTING_CAPTURE" ;
53
- case CLOSED :
54
- return "CLOSED" ;
55
-
56
- default :
57
- return "CLOSED" ;
58
- }
47
+ public CaptureState getState () {
48
+ return state ;
59
49
}
60
50
61
51
/**
@@ -111,8 +101,8 @@ public Microphone(AudioFileFormat.Type fileType) {
111
101
* @throws Exception Throws an exception if something went wrong
112
102
*/
113
103
public void captureAudioToFile (File audioFile ) throws Exception {
114
- setAudioFile (audioFile );
115
104
setState (CaptureState .STARTING_CAPTURE );
105
+ setAudioFile (audioFile );
116
106
117
107
DataLine .Info dataLineInfo = new DataLine .Info (TargetDataLine .class , getAudioFormat ());
118
108
setTargetDataLine ((TargetDataLine ) AudioSystem .getLine (dataLineInfo ));
@@ -131,9 +121,9 @@ public void captureAudioToFile(File audioFile) throws Exception {
131
121
* @throws Exception Throws an exception if something went wrong
132
122
*/
133
123
public void captureAudioToFile (String audioFile ) throws Exception {
124
+ setState (CaptureState .STARTING_CAPTURE );
134
125
File file = new File (audioFile );
135
126
setAudioFile (file );
136
- setState (CaptureState .STARTING_CAPTURE );
137
127
138
128
DataLine .Info dataLineInfo = new DataLine .Info (TargetDataLine .class , getAudioFormat ());
139
129
setTargetDataLine ((TargetDataLine ) AudioSystem .getLine (dataLineInfo ));
@@ -169,9 +159,8 @@ private AudioFormat getAudioFormat() {
169
159
* If already closed, this does nothing
170
160
*/
171
161
public void close () {
172
- if (getState (). equals ( " CLOSED" ) ) {
162
+ if (getState () == CaptureState . CLOSED ) {
173
163
} else {
174
- setState (CaptureState .CLOSED );
175
164
getTargetDataLine ().stop ();
176
165
getTargetDataLine ().close ();
177
166
}
@@ -187,12 +176,13 @@ private class CaptureThread implements Runnable {
187
176
*/
188
177
public void run () {
189
178
try {
190
- state = CaptureState .PROCESSING_AUDIO ;
179
+ setState ( CaptureState .PROCESSING_AUDIO ) ;
191
180
AudioFileFormat .Type fileType = getFileType ();
192
181
File audioFile = getAudioFile ();
193
182
getTargetDataLine ().open (getAudioFormat ());
194
183
getTargetDataLine ().start ();
195
184
AudioSystem .write (new AudioInputStream (getTargetDataLine ()), fileType , audioFile );
185
+ setState (CaptureState .CLOSED );
196
186
} catch (Exception ex ) {
197
187
ex .printStackTrace ();
198
188
}
0 commit comments