21

I am building an android app, having feature of capturing sound through microphone and playing it through headphone. For this, I have used "AudioRecord" and "AudioTrack". Following is some part of code that I am using,(just for understanding)

mInBufferSize = AudioRecord.getMinBufferSize(mSampleRate,
            AudioFormat.CHANNEL_CONFIGURATION_MONO, mFormat);
mOutBufferSize = AudioTrack.getMinBufferSize(mSampleRate,
            AudioFormat.CHANNEL_CONFIGURATION_MONO, mFormat);
mAudioInput = new AudioRecord(MediaRecorder.AudioSource.MIC,
            mSampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, mFormat,
            mInBufferSize);
mAudioOutput = new AudioTrack(AudioManager.STREAM_MUSIC, mSampleRate,
            AudioFormat.CHANNEL_CONFIGURATION_MONO, mFormat,
            mOutBufferSize, AudioTrack.MODE_STREAM);

But the main problem is that I want to record incoming sound in mp3 format? Please help me in this, I will really appreciate...

Thanks in Advance

user609239
  • 3,356
  • 5
  • 25
  • 26

6 Answers6

52

There's a work around for saving .mp3 files using MediaRecorder. Here's how:

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. Apparently MediaRecorder records playable mp3 if you're using MediaRecorder.OutputFormat.MPEG_4 and MediaRecorder.AudioEncoder.AAC together. Hope this helps somebody.

Of course, if you'd rather use the AudioRecorder class I think the source code Chirag linked below should work just fine - https://github.com/yhirano/Mp3VoiceRecorderSampleForAndroid (although you might need to translate some of it from Japanese to English)

Edit: As Bruno, and a few others pointed out in the comments, this does not encode the file in MP3. The encoding is still AAC. However, if you try to play a sound, saved using a .mp3 extension via the code above, it will still play without issues because most media players are smart enough to detect the real encoding.

Advait Saravade
  • 3,029
  • 29
  • 34
  • I am surprised that most developers aren't aware as how easy it is to record mp3! Thanks! – nat101 Jan 03 '16 at 18:48
  • Using the AudioEnconder.ACC has some weird effects on some devices...for some reason was recording audio to fast on a Lenovo Tab3 with Android 6.0. I ended up using MediaRecorder.AudioEncoder.AMR_NB and worked well for me. – velval May 23 '16 at 04:11
  • 6
    sorry, but this DOES NOT produce MP3-encoded files. they are still aac-encoded (at leaat on my galaxy s6) – Bruno Jennrich Feb 02 '17 at 10:31
  • 6
    Guys, just because you name the extension of the file MP3, it doesn't make it MP3! It's AAC encoded and devices, which don't support AAC, will not play it! So this is not a workaround and this should not be upvoted! – Krystian Mar 16 '17 at 09:59
  • you'll want to use the .m4a extension - that's the actual extension and is well accepted across most platforms (iOS, web) – Sameer J May 15 '20 at 01:05
  • Some software will not recognize a file saved like this as an mp3 file, e.g., Audacity (2022). – Luis A. Florit Apr 26 '22 at 14:31
11

Here on git you can find source code for Mp3 Voice Recorder Sample For Android .

Checkout this source code.

Chirag
  • 56,621
  • 29
  • 151
  • 198
  • That's really good answer and I am ready to accept it, but the problem is that it affects "AudioTrack", I mean after using this code, I am not able to listen sound in headphone. Can these both processes be in parellel?, like playing sound using "AudioTrack" and recording it into mp3? thanks for the answer – user609239 Aug 16 '12 at 12:59
  • You can use Media Player Class to listen it in headphone. – Chirag Aug 16 '12 at 13:00
  • Yeah, but MediaPlayer is useful after completion of recording, but In this case requirement is to play sound and record it simulataneously – user609239 Aug 16 '12 at 13:15
  • as far as I know , android under 4 do not allow you play voice at it records – Ata Oct 16 '13 at 07:14
  • Thank you for the answer. It helped me, I've found exactly what I needed. – Igor K May 23 '14 at 13:17
  • 1
    This project may be better: https://github.com/talzeus/AndroidMp3Recorder , it is with no Japanese characters. Both of them use mp3lame library. – Richard Dec 30 '14 at 08:06
  • @Richard How can I use the above link in my project? anyway to compile it via gradle etc. ?? please help – Hyder Jun 01 '17 at 07:44
  • @Sha You need to set up the compile environment on a linux system. – Richard Jun 02 '17 at 07:06
1

I know this is an old question. but if it will help someone I created a repository with an android java full recording app demo, the output file is an mp3 file. keep in mind, I set the target sdk to 21 if to eliminate the need for requesting user permission. this is only for the demo purposes. you can clone and use it.

Most of the code I used is from this repo: this

My demo repo: mp3 recording app

Eliran Azulay
  • 201
  • 4
  • 3
0

Set your outputformat of the recorder as MediaRecorder.OutputFormat.DEFAULT. I have used this and the resulting audio file is an mp3.

Nima Derakhshanjan
  • 1,380
  • 9
  • 24
  • 37
Akhil Soman
  • 2,077
  • 2
  • 17
  • 30
  • Hi. I tried this and 'adb pull'ed the result to my linux development host. File on the resultant file gives:/home/sjs/AudioDemo.mp3: ISO Media, MPEG v4 system, 3GPP. i.e. the file is an mp4 file. Device was a Nexus 7 running Android 6.0.1. Sorry & thanks. – steven smith Jun 20 '16 at 22:31
-2

This worked for me

mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setOutputFile(Environment.getExternalStorageDirectory()
    .getAbsolutePath() + "/record.mp3");
mediaRecorder.prepare();
mediaRecorder.start();
Sushant
  • 1,272
  • 2
  • 15
  • 31
-3

This is work for me

outputFile = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/myrecording.mp3";
    myAudioRecorder = new MediaRecorder();
    myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    myAudioRecorder.setOutputFile(outputFile);`

myAudioRecorder.prepare(); myAudioRecorder.start();`