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
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.
Here on git you can find source code for Mp3 Voice Recorder Sample For Android .
Checkout this source code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With