androidandroid-studioandroid-layoutandroid-mediaplayerandroid-mediarecorder

How to record good quality audio using MediaRecoder in android?


I am trying to record audio in android using media recorder . it work fines for 3gp audios, but when i try the same code with aac format it fails . (here's my code for aac format):

final String outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.acc";
final MediaRecorder myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
        myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AAC_ADTS);
        myAudioRecorder.setOutputFile(outputFile);
MediaPlayer mediaPlayer = new MediaPlayer();
                    mediaPlayer.setDataSource(outputFile);
                    mediaPlayer.prepare();
                    mediaPlayer.start();

I have added the permissions and i am triggering these functions properly using buttons. I wrote only the main code here just to make it simple My question:

What is the correct way to record and play sound in aac format ?

What is the best format to record and play audio in android using media recorder and media player and how do i implement it? I tried using 3gp but it gives low quality of sound .


Solution

  • SOLVED

    I finally figured out the correct working solution for recording good quality audio sounds myself . Here are few modifications you need to do for good quality audio recordings:

    save the file using .m4a extention. final String outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.m4a"; and do

    myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    myAudioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    myAudioRecorder.setAudioEncodingBitRate(16*44100);
    myAudioRecorder.setAudioSamplingRate(44100);
    

    Many solutions on stackoverflow would suggest .setAudioEncodingBiteRate(16) but 16 is too low and would be meaningless .

    Source: @Grant answer on stackoverflow very poor quality of audio recorded on my droidx using MediaRecorder, why?

    Edit: The only reason i wanted to save file with .aac extension was my assumption that .aac files would be more clearer, but since i am able to save files in good quality without .aac extension there's no need to go for it .