Reputation: 2655
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 .
Upvotes: 0
Views: 6241
Reputation: 2655
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 .
Upvotes: 6
Reputation: 11
If you use a audioRecord object instead of a mediaRecorder object than you have more freedom to customize how you record and encode the output from the microphone(s).
AudioRecord does take longer to understand and implement though, if you want to stick with a mediaRecorder object then I recommend using AAC_ADTS as the audio encoder and allowing the outputFormat to be THREE_GPP.
otherwise, this may help you encode raw AAC files:
How to generate the AAC ADTS elementary stream with Android MediaCodec
Upvotes: 1