Android : Record sound in mp3 format
Asked Answered
P

6

22

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

Parasol answered 16/8, 2012 at 10:45 Comment(0)
P
51

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.

Peacock answered 10/10, 2015 at 14:1 Comment(6)
I am surprised that most developers aren't aware as how easy it is to record mp3! Thanks!Dispersoid
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.Neurogenic
sorry, but this DOES NOT produce MP3-encoded files. they are still aac-encoded (at leaat on my galaxy s6)Capacitor
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!Lepine
you'll want to use the .m4a extension - that's the actual extension and is well accepted across most platforms (iOS, web)Empurple
Some software will not recognize a file saved like this as an mp3 file, e.g., Audacity (2022).Bellona
S
11

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

Checkout this source code.

Social answered 16/8, 2012 at 10:58 Comment(8)
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 answerParasol
You can use Media Player Class to listen it in headphone.Social
Yeah, but MediaPlayer is useful after completion of recording, but In this case requirement is to play sound and record it simulataneouslyParasol
as far as I know , android under 4 do not allow you play voice at it recordsAlikee
Thank you for the answer. It helped me, I've found exactly what I needed.Amerson
This project may be better: github.com/talzeus/AndroidMp3Recorder , it is with no Japanese characters. Both of them use mp3lame library.Subspecies
@Subspecies How can I use the above link in my project? anyway to compile it via gradle etc. ?? please helpFlogging
@Sha You need to set up the compile environment on a linux system.Subspecies
A
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

Aleydis answered 13/11, 2019 at 18:7 Comment(0)
R
0

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

Rog answered 10/11, 2015 at 5:25 Comment(1)
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.Amyloid
H
-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();
Horsehide answered 5/6, 2016 at 7:52 Comment(0)
I
-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();`

Insure answered 28/3, 2015 at 8:52 Comment(1)
This doesn't record mp3, only the extension of the file will be mo3, not the file format.Hausa

© 2022 - 2024 — McMap. All rights reserved.