PauseResumeAudioRecorder { public void startRecording(){ if (currentAudioState.get() == PREPARED_STATE) { currentAudioRecordingThread = new AudioRecorderThread(audioFile.replace(".wav",".temp"), MediaRecorder.AudioSource.MIC, sampleRateInHertz,channelConfig,audioEncoding,maxFileSizeInBytes); currentAudioState.set(RECORDING_STATE); currentAudioRecordingThread.start(); onTimeCompletedTimer=new Timer(true); onTimeCompletionTimerTask=new MaxTimeTimerTask(); onTimeCompletedTimer.schedule(onTimeCompletionTimerTask,maxTimeInMillis); remainingMaxTimeInMillis=maxTimeInMillis; recordingStartTimeMillis=System.currentTimeMillis(); } else{ Log.w(TAG,"Audio recorder is not in prepared state. Ignoring call."); } } 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 testStartRecording()throws NoSuchFieldException, IllegalAccessException,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(); Assert.assertNotNull("Recording thread is not created", currentAudioRecordingThreadField.get(pauseResumeAudioRecorder)); Assert.assertEquals("Correct state not set", PauseResumeAudioRecorder.RECORDING_STATE, pauseResumeAudioRecorder.getCurrentState()); Assert.assertNotNull("Timer is null", onTimeCompletedTimerField.get(pauseResumeAudioRecorder)); Assert.assertNotNull("TimerTask is null",onTimeCompletionTimerTaskField.get(pauseResumeAudioRecorder)); Assert.assertTrue("start time not set",((long)recordingStartTimeMillisField.get(pauseResumeAudioRecorder))>=0); 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); Assert.assertTrue("AudioRecord is not recording", shadowAudioRecord.isRecording); Assert.assertEquals("AudioRecord has wrong sample rate", 44100, shadowAudioRecord.sampleRateInHz); Assert.assertEquals("AudioRecord has wrong audio encoding", AudioFormat.ENCODING_PCM_8BIT, shadowAudioRecord.audioFormat); Assert.assertEquals("AudioRecord has wrong audio source", MediaRecorder.AudioSource.MIC,shadowAudioRecord.audioSource); Assert.assertEquals("AudioRecord has wrong channel config", AudioFormat.CHANNEL_IN_STEREO, shadowAudioRecord.channelConfig); final File pcmFile=new File(Environment.getExternalStorageDirectory() + "/recording.temp"); Assert.assertTrue(pcmFile.exists()); pcmFile.delete(); }