PauseResumeAudioRecorder { public void stopRecording(){ if (currentAudioState.get()== PAUSED_STATE || currentAudioState.get()==RECORDING_STATE){ currentAudioState.getAndSet(STOPPED_STATE); onTimeCompletedTimer.cancel(); onTimeCompletedTimer=null; onTimeCompletionTimerTask=null; } else{ Log.w(TAG,"Audio recording is not in a paused or recording state."); } currentAudioRecordingThread=null; } 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 testStopRecording()throws IllegalAccessException,NoSuchFieldException, InterruptedException{ 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(); Thread.sleep(100); audioRecordThreadField=currentAudioRecordingThreadField.get(pauseResumeAudioRecorder).getClass().getDeclaredField("currentAudioRecording"); audioRecordThreadField.setAccessible(true); AudioRecord audioRecord=(AudioRecord)audioRecordThreadField.get(currentAudioRecordingThreadField.get(pauseResumeAudioRecorder)); ShadowAudioRecord shadowAudioRecord=Robolectric.shadowOf_(audioRecord); pauseResumeAudioRecorder.stopRecording(); Thread.sleep(5000); Assert.assertEquals("Wrong state was set", PauseResumeAudioRecorder.STOPPED_STATE, pauseResumeAudioRecorder.getCurrentState()); Assert.assertFalse("PCM file still exists", new File(Environment.getExternalStorageDirectory() + "/recording.pcm").exists()); Assert.assertTrue("WAV file does not exist", new File(Environment.getExternalStorageDirectory() + "/recording.wav").exists()); Assert.assertFalse("AudioRecord is still recording",shadowAudioRecord.isRecording); new File(Environment.getExternalStorageDirectory() + "/recording.wav").delete(); }