Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to produce .WAV file using MediaRecorder class?

I had some trouble with creating certain file types.

 MediaRecorder myAudioRecorder=new MediaRecorder();
 myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
 myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
 myAudioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
 myAudioRecorder.setAudioEncodingBitRate(16);

Can you explain me which one of these I need to use combined in order to produce regular MP3, WAV, AAC or AMR audio files. Now I'm unsure if that's a correct way :(

like image 662
Bramastic Avatar asked Oct 29 '25 15:10

Bramastic


1 Answers

You can't produce wav or mp3 using MediaRecorder. Using MediaRecorder you can produce 3gp, aac and m4a. I am using these code blocks to record audio from microphone:

    if (Build.VERSION.SDK_INT <= 9) {
        // Android 2.3.2 and lower
        outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "XXX"
                + File.separator + strDate + ".3gp";
        mRecorder.setAudioSamplingRate(8000);
        mRecorder.setAudioEncodingBitRate(12200);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    } else if (Build.VERSION.SDK_INT >= 10 && Build.VERSION.SDK_INT <= 15) {
        // Android 2.3.3 - Android 4.0.4
        outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "XXX"
                + File.separator + strDate + ".m4a";
        mRecorder.setAudioSamplingRate(44100);
        mRecorder.setAudioEncodingBitRate(96000);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    } else {
        // Android 4.1 and above
        outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "XXX"
                + File.separator + strDate + ".m4a";
        mRecorder.setAudioSamplingRate(44100);
        mRecorder.setAudioEncodingBitRate(96000);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
    }

Note: When I try to produce aac sound that file can't be playable on Android but not playable on Windows. I'm not sure why but m4a is also produce quality sounds.

Edit: m4a and 3gp files play perfectly on Android 4.x. Particularly I tested on Android 4.4.2 Samsung Galaxy 4.4.2 and both output format play without problem. However I couldn't play m4a file on Android 5.x and Android 6.x. aac file is doesn't play all three platform. But all files (3gp, m4a, aac) play on Windows 10. I have no idea why.

like image 99
Aykut Uludağ Avatar answered Nov 01 '25 04:11

Aykut Uludağ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!