MediaRecorder.Stop() fails on Samsung devices such as Galaxy A7, Galaxy A10s etc

Viewed 303

I am using the following settings to initialize MediaRecorder. My app is working fine on most devices except Samsung and an error occurs on MediaRecorder.stop().

    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    recorder.setAudioEncodingBitRate(16*44100);
    recorder.setAudioSamplingRate(44100);

it throws the following error message.

java.lang.IllegalStateException: 
at android.media.MediaRecorder._stop (MediaRecorder.java)
at android.media.MediaRecorder.stop (MediaRecorder.java:1440)
at com.appname.stopRecording (add_new.java:4)`

Could this be because I am using MIC as an audio source for Samsung devices instead of VOICE_CALL?

1 Answers

Try doing it this way.

recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
recorder.setAudioEncodingBitRate(16*44100);
recorder.setAudioSamplingRate(44100);
recorder.prepare();
recorder.start();   // Recording is now started

And when you are trying to stop, do it like this if you arent already.

recorder.stop();
recorder.reset();   // You can reuse the object by going back to setAudioSource() step
recorder.release(); // Now the object cannot be reused

So, if you call recorder.stop(); before calling recorder.start(); if will show the exception. And also please check if you have added the permission in manifest:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
Related