Android MediaRecorder not working in some devices

Viewed 2196

I am running Media Recorder in background service which is running perfect in Samsung 6.0 and above devices, but it is not working in lower devices. Throwing the below exception

stop called in an invalid state: 4

FATAL EXCEPTION: main
Process: com.shoaibnwar.crighter, PID: 28547
java.lang.IllegalStateException
    at android.media.MediaRecorder._stop(Native Method)
    at android.media.MediaRecorder.stop(MediaRecorder.java:946)
    at com.shoaibnwar.crighter.Services.RecordingAudio.stopRecording(RecordingAudio.java:114)
    at com.shoaibnwar.crighter.Services.RecordingAudio.access$000(RecordingAudio.java:24)
    at com.shoaibnwar.crighter.Services.RecordingAudio$1.run(RecordingAudio.java:69)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:7007)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

Here is my start recording code

private void startRecording(File file) {

    if (mediaRecorder != null) {
        mediaRecorder.reset();
        mediaRecorder.release();
    }
    mediaRecorder = new MediaRecorder();
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    //mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_2_TS);
   // mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
    //mediaRecorder.setOutputFile(file.getAbsolutePath());

    mediaRecorder.setOutputFile(file.getAbsolutePath());
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

   /* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
        mediaRecorder.setAudioEncodingBitRate(48000);
    } else {
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        mediaRecorder.setAudioEncodingBitRate(64000);
    }
    mediaRecorder.setAudioSamplingRate(16000);*/
    try {
        mediaRecorder.prepare();
        mediaRecorder.start();
    } catch (IOException e) {
        Log.e("giftlist", "io problems while preparing [" +
                file.getAbsolutePath() + "]: " + e.getMessage());
    }
}

and stop recording function is

private void stopRecording() {
    if (mediaRecorder != null) {
        mediaRecorder.stop();
        mediaRecorder.release();
        mediaRecorder = null;
    }
}

i am setting the file path like bellow

private File getOutputFile() {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmssSSS", Locale.US);

    return new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()
            +File.separator
            +Environment.DIRECTORY_MUSIC
            +File.separator
            + "/CRighterVoice/RECORDING_"
            + dateFormat.format(new Date())
            + ".m4a");


}
2 Answers

I had the same issues. What I got from search is that basic problem is in
mediaRecorder.setAudioSource() because you are using AudioSource.MIC so some device does not support this.

There are some Audio recording sources we can use while preparing the recording doc here, but sadly in each different device, it might be different.

For some devices, VOICE_CALL works, and for some, others. But at least we can try. So in my case, a device which is running Android below 6, VOICE_CALL is working fine.

Another reason is that Sometimes prepare takes some time to complete. I've found this sample project here, which for some reason sleeps for 2 seconds on the UI thread between prepare and start calls of the mediaRecorder. It works fine.

Following is the link of the project that record call successfully you see and check how the media source is changed in all devices.

Hope this solve your problem.

Try calling

mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

before

mediaRecorder.setOutputFile(file.getAbsolutePath());
Related