18

I am creating an app that contains voice recording and playing. Is it possible to record files in MP3 format?

I need output file as mp3 format. Thanks.

3
  • FYI: MP3 technology is patented. If you're intending to use MP3 to reduce your local storage usage (and assuming this is to be a commercial app), you'd be better off using something like Ogg Vorbis. Feb 3, 2011 at 13:20
  • 1
    Hi, in android i am recording voice in raw stream and convert into wav. but in wav takes more size i need to reduce the size so that i want to convert into wav to mp3. i saw the stackoverflow.com/questions/3641920/… here but i do not know how to use it android please help me.
    – M.A.Murali
    Jun 1, 2012 at 6:19
  • Possible duplicate of Android : Record sound in mp3 format
    – grebulon
    Feb 15, 2016 at 11:02

3 Answers 3

15

There's currently no MP3 encoder built into the Android framework (as far as I know), so you can't do it out of the box. You need to add an MP3 encoding library to your project to do so.

For this, you can look at this stackoverflow post for a complete answer.

3
  • Hi, in android i am recording voice in raw stream and convert into wav. but in wav takes more size i need to reduce the size so that i want to convert into wav to mp3. i saw the link posted here but i do not know how to use it android please help me.
    – M.A.Murali
    Jun 1, 2012 at 6:18
  • Is this still true today in 2021? MP3 has been around for yonks and I heard patent/license free since 2017.
    – Phil
    Sep 9, 2021 at 14:28
  • @Phil If you take a look at the Supported Media Formats (developer.android.com/guide/topics/media/media-formats), you'll see that MP3 still doesn't have an encoder on Android. Only a decoder.
    – Lior
    Sep 11, 2021 at 1:17
8

2015 update:

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

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

Core Media Formats: MP3 • Mono/Stereo 8-320Kbps constant (CBR) or variable bit-rate (VBR) MP3 (.mp3)

  • (edit) - For smaller file sizes, you can use an .mp4. You can set it up with:

    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

8
  • 2
    I don't think recording is supported, because I don't see a dot in the encoding side.
    – Eric Kim
    Dec 12, 2015 at 1:12
  • Its supported. Try it.
    – David
    Dec 12, 2015 at 10:40
  • en.m.wikipedia.org/wiki/Comparison_of_video_container_formats It can holdl mp3 files - MPEG-2/4 (HE)-AAC, MPEG-1/2 Layers I, II, III (MP3), AC-3, Apple Lossless, ALS, SLS, Vorbis(with private objectTypeIndication), others[26]
    – David
    Dec 13, 2015 at 9:51
  • I think this is saying that mp3 encoding can be stored in the mpeg wrapper. Is that correct?
    – David
    Dec 13, 2015 at 9:55
  • 2
    Just because your file extension is .mp3 and you can play it does not make it proper mp3 file. I am pretty sure Android doesn't do this out of the box.
    – Eric Kim
    Dec 14, 2015 at 20:16
8

There is definitely an MP3 encoder on the Android framework.

Like I answered here on stackoverflow all you need 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 the setOuputFormat and the setAudioEncoder. MediaRecorder records playable mp3 if you're using MediaRecorder.OutputFormat.MPEG_4 and MediaRecorder.AudioEncoder.AAC together.

The funny thing is that this solution was a result of my experimenting with what works, and I have no idea as to why it works. I was hoping somebody could shed some light on this in the comments.

EDIT

As @JohnSmith and a few other pointed out in the comments, this does not encode the audio in MP3 format and there is no in-built MP3 encoder on Android.

The reason this solution seems to work is that MediaPlayer recognizes the encoding as AAC before beginning the track (regardless of the file extension). So although the output file's mime type is audio/mp4, the encoding is most definitely AAC.

5
  • It doesn't create the actual MP3 file, it just creates a audio file with extension as MP3 but it's not a MP3 actually.
    – Harpreet
    Mar 15, 2016 at 6:49
  • Actually the mimetype returned by the created file is audio/mp4. It does sound like an mp3 but i noticed its not the same. Apr 5, 2016 at 23:58
  • 3
    This answer is definitely incorrect. The reason it SEEMS to work is most players are smart enough to figure out that just because the file is called myrecording.mp3, it is actually an .aac encoded audio stream in a .mp4 container. Mp3 is not native in Android (no doubt due to copyright) You are just faking an m4a or an ACC which is NOT mp3. This best method I found which worked unbelievably well for me in my app is to use the Java lame library github.com/nwaldispuehl/java-lame.
    – John Smith
    Nov 11, 2016 at 18:38
  • As @JohnSmith and others have rightly pointed out, Android does not have an MP3 encoder built in, and the file that the above code produces is not MP3 encoded. Thanks for sharing the Lame lib ported for Java John, it's the best resource for encoding in MP3. Aug 12, 2017 at 14:58
  • this is aac codec, what are you doing, please remove you answer, don't answer if you don't know what are you talking about, many unprofessional devs will copy your code
    – user924
    Feb 7 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.