Android: how to get the output of a media recorder in MP3 format?

I am creating an application that contains voice recording and playback. Can I record MP3 files?

I need an output file in mp3 format. Thanks.

+8
source share
3 answers

There is currently no MP3 encoder built into the Android infrastructure (as far as I know), so you cannot do this out of the box. To do this, you need to add the MP3 encoding library to your project.

For this, you can find this postoverflow post for a complete answer.

+10
source

2015 update:

MP3 is officially supported by android - (edit) - however, it is only for decoding, not for encoding.

http://developer.android.com/guide/appendix/media-formats.html

Main media formats: MP3 • Mono / Stereo 8-320 Kbps constant (CBR) or variable bit rate (VBR) MP3 (.mp3)

  • (change). For smaller file sizes you can use .mp4. You can configure it with:

    mRecorder.setOutputFormat (MediaRecorder.OutputFormat.MPEG_4);

+8
source

Android definitely has an MP3 encoder.

As I replied to https://stackoverflow.com/a/312960/409 , all you have to do is:

MediaRecorder recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); recorder.setOutputFile(Environment.getExternalStorageDirectory() .getAbsolutePath() + "/myrecording.mp3"); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); recorder.prepare(); recorder.start(); 

The important part here is setOuputFormat and setAudioEncoder . MediaRecorder records playable mp3s if you use MediaRecorder.OutputFormat.MPEG_4 and MediaRecorder.AudioEncoder.AAC together.

The funny thing is that this solution was the result of my experiments with what works, and I have no idea why this works. I was hoping someone could shed some light on this in the comments.

EDIT

As @JohnSmith and several others pointed out in the comments, this does not encode MP3 audio, and Android does not have a built-in MP3 encoder.

The reason this solution works is because MediaPlayer recognizes the encoding as AAC before the start of the track (regardless of the file extension). Thus, although the mime file type of the output file is audio / mp4, the encoding is certainly AAC.

+4
source

All Articles