PauseResumeAudioRecorder { public void pauseRecording(){ if (currentAudioState.get()==RECORDING_STATE){ currentAudioState.getAndSet(PAUSED_STATE); onTimeCompletedTimer.cancel(); remainingMaxTimeInMillis=remainingMaxTimeInMillis-(System.currentTimeMillis()-recordingStartTimeMillis); } else{ Log.w(TAG,"Audio recording is not recording"); } } PauseResumeAudioRecorder(); void setOnFileSizeReachedListener(final OnFileSizeReachedListener onFileSizeReachedListener); void setMaxFileSizeInBytes(final long maxFileSizeInBytes); void setOnTimeCompletionListener(final OnTimeCompletionListener onTimeCompletionListener); void setMaxTimeInMillis(final long maxTimeInMillis); void setAudioEncoding(final int audioEncoding); void setAudioFile(final String audioFilePath); void setSampleRate(final int sampleRateInHertz); void setChannel(final int channelConfig); int getCurrentState(); void startRecording(); void pauseRecording(); void resumeRecording(); void stopRecording(); static final int STOPPED_STATE; static final int INITIALIZED_STATE; static final int PREPARED_STATE; static final int RECORDING_STATE; static final int PAUSED_STATE; static final int ERROR_STATE; }
@Test public void testPauseRecording()throws InterruptedException,IllegalAccessException{ PauseResumeAudioRecorder pauseResumeAudioRecorder=new PauseResumeAudioRecorder(); pauseResumeAudioRecorder.setAudioFile(Environment.getExternalStorageDirectory() + "/recording.wav"); pauseResumeAudioRecorder.setChannel(AudioFormat.CHANNEL_IN_STEREO); pauseResumeAudioRecorder.setSampleRate(44100); pauseResumeAudioRecorder.setAudioEncoding(AudioFormat.ENCODING_PCM_8BIT); pauseResumeAudioRecorder.startRecording(); onTimeCompletedTimerField.set(pauseResumeAudioRecorder, mockTimer); Thread.sleep(100); pauseResumeAudioRecorder.pauseRecording(); Thread.sleep(200); long remainingTimeMillis=((long) remainingMaxTimeInMillisField.get(pauseResumeAudioRecorder)); Assert.assertTrue("wrong time:"+remainingTimeMillis,(remainingTimeMillis<=(PcmWavConverter.MAX_TIME_WAV_FILE_MILLIS-100)) &&remainingTimeMillis>(PcmWavConverter.MAX_TIME_WAV_FILE_MILLIS-5000)); Mockito.verify(mockTimer,Mockito.times(1)).cancel(); Assert.assertEquals("Correct state not set", PauseResumeAudioRecorder.PAUSED_STATE, pauseResumeAudioRecorder.getCurrentState()); final File pcmFile=new File(Environment.getExternalStorageDirectory() + "/recording.pcm"); pcmFile.delete(); Thread.sleep(100); Assert.assertFalse("Thread is not paused", pcmFile.exists()); }
@Test public void testPauseRecordingWhilePaused()throws IllegalAccessException{ PauseResumeAudioRecorder pauseResumeAudioRecorder=new PauseResumeAudioRecorder(); currentAudioStateField.set(pauseResumeAudioRecorder, new AtomicInteger(PauseResumeAudioRecorder.PAUSED_STATE)); pauseResumeAudioRecorder.pauseRecording(); new File(Environment.getExternalStorageDirectory() + "/recording.pcm").delete(); }