|
26 | 26 | import omrecorder.Recorder; |
27 | 27 |
|
28 | 28 | public class AudioRecorderActivity extends AppCompatActivity |
29 | | - implements PullTransport.OnAudioChunkPulledListener, |
30 | | - MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener { |
| 29 | + implements PullTransport.OnAudioChunkPulledListener, MediaPlayer.OnCompletionListener { |
31 | 30 |
|
32 | 31 | private MediaPlayer player; |
33 | 32 | private Recorder recorder; |
@@ -38,7 +37,8 @@ public class AudioRecorderActivity extends AppCompatActivity |
38 | 37 |
|
39 | 38 | private Timer timer; |
40 | 39 | private MenuItem saveMenuItem; |
41 | | - private int secondsElapsed; |
| 40 | + private int recorderSecondsElapsed; |
| 41 | + private int playerSecondsElapsed; |
42 | 42 | private boolean isRecording; |
43 | 43 |
|
44 | 44 | private RelativeLayout contentLayout; |
@@ -120,7 +120,7 @@ public void onResume() { |
120 | 120 |
|
121 | 121 | @Override |
122 | 122 | protected void onPause() { |
123 | | - stopRecording(); |
| 123 | + restartRecording(null); |
124 | 124 | try { |
125 | 125 | visualizerView.onPause(); |
126 | 126 | } catch (Exception e){ } |
@@ -168,19 +168,13 @@ public void onAudioChunkPulled(AudioChunk audioChunk) { |
168 | 168 | visualizerHandler.onDataReceived(amplitude); |
169 | 169 | } |
170 | 170 |
|
171 | | - @Override |
172 | | - public void onPrepared(final MediaPlayer mediaPlayer) { |
173 | | - if(mediaPlayer != null){ |
174 | | - |
175 | | - } |
176 | | - } |
177 | | - |
178 | 171 | @Override |
179 | 172 | public void onCompletion(MediaPlayer mediaPlayer) { |
180 | 173 | stopPlaying(); |
181 | 174 | } |
182 | 175 |
|
183 | 176 | private void selectAudio() { |
| 177 | + stopRecording(); |
184 | 178 | setResult(RESULT_OK); |
185 | 179 | finish(); |
186 | 180 | } |
@@ -191,16 +185,16 @@ public void toggleRecording(View v) { |
191 | 185 | @Override |
192 | 186 | public void run() { |
193 | 187 | if (isRecording) { |
194 | | - stopRecording(); |
| 188 | + pauseRecording(); |
195 | 189 | } else { |
196 | | - startRecording(); |
| 190 | + resumeRecording(); |
197 | 191 | } |
198 | 192 | } |
199 | 193 | }); |
200 | 194 | } |
201 | 195 |
|
202 | 196 | public void togglePlaying(View v){ |
203 | | - stopRecording(); |
| 197 | + pauseRecording(); |
204 | 198 | Util.wait(100, new Runnable() { |
205 | 199 | @Override |
206 | 200 | public void run() { |
@@ -217,44 +211,48 @@ public void restartRecording(View v){ |
217 | 211 | stopRecording(); |
218 | 212 | stopPlaying(); |
219 | 213 | saveMenuItem.setVisible(false); |
| 214 | + statusView.setVisibility(View.INVISIBLE); |
220 | 215 | restartView.setVisibility(View.INVISIBLE); |
221 | 216 | playView.setVisibility(View.INVISIBLE); |
222 | 217 | recordView.setImageResource(R.drawable.aar_ic_rec); |
223 | 218 | timerView.setText("00:00:00"); |
224 | | - secondsElapsed = 0; |
| 219 | + recorderSecondsElapsed = 0; |
| 220 | + playerSecondsElapsed = 0; |
225 | 221 | } |
226 | 222 |
|
227 | | - private void startRecording() { |
| 223 | + private void resumeRecording() { |
228 | 224 | isRecording = true; |
229 | 225 | saveMenuItem.setVisible(false); |
230 | | - timerView.setText("00:00:00"); |
231 | 226 | statusView.setText(R.string.aar_recording); |
232 | 227 | statusView.setVisibility(View.VISIBLE); |
233 | 228 | restartView.setVisibility(View.INVISIBLE); |
234 | 229 | playView.setVisibility(View.INVISIBLE); |
235 | | - recordView.setImageResource(R.drawable.aar_ic_stop); |
| 230 | + recordView.setImageResource(R.drawable.aar_ic_pause); |
236 | 231 | playView.setImageResource(R.drawable.aar_ic_play); |
237 | 232 |
|
238 | 233 | visualizerHandler = new VisualizerHandler(); |
239 | 234 | visualizerView.linkTo(visualizerHandler); |
240 | 235 |
|
241 | | - recorder = OmRecorder.wav( |
242 | | - new PullTransport.Default(Util.getMic(), AudioRecorderActivity.this), |
243 | | - new File(filePath)); |
244 | | - recorder.startRecording(); |
| 236 | + if(recorder == null) { |
| 237 | + timerView.setText("00:00:00"); |
| 238 | + |
| 239 | + recorder = OmRecorder.wav( |
| 240 | + new PullTransport.Default(Util.getMic(), AudioRecorderActivity.this), |
| 241 | + new File(filePath)); |
| 242 | + } |
| 243 | + recorder.resumeRecording(); |
245 | 244 |
|
246 | 245 | startTimer(); |
247 | 246 | } |
248 | 247 |
|
249 | | - private void stopRecording() { |
| 248 | + private void pauseRecording() { |
250 | 249 | isRecording = false; |
251 | 250 | if(!isFinishing()) { |
252 | 251 | saveMenuItem.setVisible(true); |
253 | 252 | } |
254 | | - statusView.setText(""); |
255 | | - statusView.setVisibility(View.INVISIBLE); |
256 | | - // TODO pause and restart recording before showing this button |
257 | | -// restartView.setVisibility(View.VISIBLE); |
| 253 | + statusView.setText(R.string.aar_paused); |
| 254 | + statusView.setVisibility(View.VISIBLE); |
| 255 | + restartView.setVisibility(View.VISIBLE); |
258 | 256 | playView.setVisibility(View.VISIBLE); |
259 | 257 | recordView.setImageResource(R.drawable.aar_ic_rec); |
260 | 258 | playView.setImageResource(R.drawable.aar_ic_play); |
@@ -265,16 +263,25 @@ private void stopRecording() { |
265 | 263 | visualizerHandler.stop(); |
266 | 264 | } |
267 | 265 |
|
| 266 | + if (recorder != null) { |
| 267 | + recorder.pauseRecording(); |
| 268 | + } |
| 269 | + |
| 270 | + stopTimer(); |
| 271 | + } |
| 272 | + |
| 273 | + private void stopRecording(){ |
| 274 | + recorderSecondsElapsed = 0; |
268 | 275 | if (recorder != null) { |
269 | 276 | recorder.stopRecording(); |
270 | 277 | recorder = null; |
271 | 278 | } |
272 | | - |
273 | 279 | stopTimer(); |
274 | 280 | } |
275 | 281 |
|
276 | 282 | private void startPlaying(){ |
277 | 283 | try { |
| 284 | + stopRecording(); |
278 | 285 | player = new MediaPlayer(); |
279 | 286 | player.setDataSource(filePath); |
280 | 287 | player.prepare(); |
@@ -291,8 +298,9 @@ public void run() { |
291 | 298 | timerView.setText("00:00:00"); |
292 | 299 | statusView.setText(R.string.aar_playing); |
293 | 300 | statusView.setVisibility(View.VISIBLE); |
294 | | - playView.setImageResource(R.drawable.aar_ic_pause); |
| 301 | + playView.setImageResource(R.drawable.aar_ic_stop); |
295 | 302 |
|
| 303 | + playerSecondsElapsed = 0; |
296 | 304 | startTimer(); |
297 | 305 | } catch (Exception e){ |
298 | 306 | e.printStackTrace(); |
@@ -308,7 +316,6 @@ private void stopPlaying(){ |
308 | 316 | try { |
309 | 317 | player.stop(); |
310 | 318 | player.reset(); |
311 | | - player.release(); |
312 | 319 | } catch (Exception e){ } |
313 | 320 | } |
314 | 321 |
|
@@ -325,7 +332,6 @@ private boolean isPlaying(){ |
325 | 332 |
|
326 | 333 | private void startTimer(){ |
327 | 334 | stopTimer(); |
328 | | - secondsElapsed = 0; |
329 | 335 | timer = new Timer(); |
330 | 336 | timer.scheduleAtFixedRate(new TimerTask() { |
331 | 337 | @Override |
@@ -339,18 +345,22 @@ private void stopTimer(){ |
339 | 345 | if (timer != null) { |
340 | 346 | timer.cancel(); |
341 | 347 | timer.purge(); |
| 348 | + timer = null; |
342 | 349 | } |
343 | 350 | } |
344 | 351 |
|
345 | 352 | private void updateTimer() { |
346 | | - if(isRecording || isPlaying()) { |
347 | | - runOnUiThread(new Runnable() { |
348 | | - @Override |
349 | | - public void run() { |
350 | | - secondsElapsed++; |
351 | | - timerView.setText(Util.formatSeconds(secondsElapsed)); |
| 353 | + runOnUiThread(new Runnable() { |
| 354 | + @Override |
| 355 | + public void run() { |
| 356 | + if(isRecording) { |
| 357 | + recorderSecondsElapsed++; |
| 358 | + timerView.setText(Util.formatSeconds(recorderSecondsElapsed)); |
| 359 | + } else if(isPlaying()){ |
| 360 | + playerSecondsElapsed++; |
| 361 | + timerView.setText(Util.formatSeconds(playerSecondsElapsed)); |
352 | 362 | } |
353 | | - }); |
354 | | - } |
| 363 | + } |
| 364 | + }); |
355 | 365 | } |
356 | 366 | } |