Search code examples
androidmediarecorder

android - Which MediaRecorder configuration is supported by all device?


I am recording communication voice in my app and I added storage and audio record permission manifest and also getting programmatically.

My code is working fine on one device(Android 6.0 Lenovo K3 Note) But not on another (Android 8.1 ONEPLUS A5010)

In second device output is saved as a blank file of 3.15KB

I am adding my code which I am using please tell what I am doing wrong.

 MediaRecorder mRecorder;
 String mFileName;

Code in OnCreate

 File file = new File(getFilesDir(), "engwingoLastCall.3gp");

                 mFileName = file.getAbsolutePath();

try {
        if(mRecorder == null) {
            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setOutputFile(mFileName);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        }
    } catch (Exception e) {
        Log.d(TAG,"Recorder Error:"+e.getMessage());
    }

Methods

public void startRecording() {

    try {
        if(mRecorder != null) {
            mRecorder.prepare();
            mRecorder.start();
        }
    } catch (Exception e) {
        Log.d("Recorder", "prepare() failed");

    }


}
public void stopRecording() {

    if(mRecorder != null) {

        try {
            mRecorder.stop();
            mRecorder.release();
            mRecorder = null;
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }catch (Exception e){
            Log.d(TAG,e.getMessage());

        }
    }
}

Solution

  • My Code was OK but the reason for this behavior of my recorder was, Some other service also using my recorder at that time and that's why the file was saved empty (3.15KB Size)