Android audio recording, part 2

Download source code of this article.

My previous article was about recording audio using MediaRecorder class. This class is very easy to use and can be used for quick and simple audio recording, also this class saves the audio data either MPEG 4 or 3GPP compressed format. But if you need raw data that can be used to process audio data, then you cannot use MediaRecorder class but you have to use AudioRecord class. AudioRecord class provides you the raw data in uncompressed format. You can use this data to write to a file, display waveform, etc..

To record audio using AudioRecord class:

  1. Create an instance of AudioRecord
  2. Start recording using startRecording() method
  3. Read uncompressed data using method
  4. Stop recording using stop() method.

Creating AudioRecord instance

The constructor of AudioRecord class is:

	int audioSource, int sampleRateInHz,
	int channelConfig, int audioFormat, int bufferSizeInBytes)

The first parameter is the audio source, this can be one of the AudioSource values. Second parameter is the sample rate in Hz. This can be 44100, 22050, 11025, 8000, etc.. Third parameter is channel value, this can be one of the AudioFormat values (normally CHANNEL_IN_MONO or CHANNEL_IN_STEREO). Third is the audio encoder format, this can be one of the following values:

  1. ENCODING_PCM_16BIT – 16 bit per sample
  2. ENCODING_PCM_8BIT – 8 bit per sample
  3. ENCODING_DEFAUL – default encoding

Forth parameter is the buffer size. This should be calculated using AudioRecord.getMinBufferSize() static method. The syntax of getMinBufferSize() method is:

public static int getMinBufferSize (
	int sampleRateInHz, int channelConfig, int audioFormat)

The parameters are same as that of the constructor. The method calculate the minimum buffer size needed to store one chunk of audio. Anything less than this number will result in failure while creating the AudioRecord object.

Start Recording

Once we create an instance of the AudioRecord object, startRecording() method is used to start recording audio.

Reading uncompressed data

Once the recording is started, it is the responsibility of the application to read the audio data and store it for further processing. We can read the audio data using one of the read method of AudioRecord class. The audio data will be raw PCM_16BIT or PCM_8BIT format depending on how you initialized the object. The application can use this data to do further processing of any sort. In our sample application we just save this data to a temporary file, further when the recording is stopped we read this data and write to WAV file. The application has to continuously read the data otherwise the previous chunk of data will be overwritten by the new one.

Stop recording

Recording can be stopped using stop() method. Remember to call release() method to release the AudioRecord object.

The following code snippet shows the typical usage:

AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
	44100, AudioFormat.CHANNEL_IN_STEREO,
	AudioFormat.ENCODING_PCM_16BIT, bufferSize);


Sample application

The sample application of this article records audio and save it to a WAV file. The WAV file will be placed in “/SDCard/AudioRecorder” folder with current millisecond as the file name.


Writing WAV file – RingDroid sample application, RehearsalAssist application

58 Responses

  1. sudheer January 25, 2011 / 12:23 pm


    Thanks for sharing nice tutorial.

    I am also working on the similar application. Here I am trying to send the audio to an IP camera over http post method. I tried with media recorder but i am unable to play it in IP camera.

    IP camera have some supported formats like G711 and G726 audio compression formats. Now the question is how can i do it with your sample application. Kindly help me to do so.

    Thanks & Regards

    • Krishnaraj Varma January 26, 2011 / 8:04 am

      Sudheer, this is a complicated issue and I don’t have any specific solution for that. But this most probably happens because of the format difference. MediaRecorder uses AMR encoder and can save files in MPEG4, Raw AMR and 3GPP formats. I am not sure how you send the data to the remote player, when the recording is finished you are sending the file content?

      I suggest you to try AudioRecord and send the uncompressed data to the remote player.

      Update here your progress. Thanks.

      -Krishnaraj Varma

  2. Ajit kumar February 1, 2011 / 5:55 am

    Hi, I was able to record. but.. while playing, it sounds like it has been fast forwarded. can you suggest me of what to do on this. I am doing it on LG optimus. app is specifically for this device only. and one more thing, will the recording quality be fine even when device has reached time out on display? i tried with Media player but when it goes to time out, sound recording is not proceeding. please help me on this

    • Krishnaraj Varma February 6, 2011 / 9:05 am

      Ajit, I think the fast forward issue may be related to writing to WAV file, please check WAV header and see whether you are specifying correct values.

      Could you please explain the timeout issue you are getting, I didn’t get the correctly.

      • Kyle Ivey February 14, 2011 / 9:25 pm

        I also experienced high-pitched voices when testing this code. I made the recordings with settings configured for 8000Hz, Mono, 16Bit PCM. I discovered that the sample rate of the recordings was exactly 2x what I had configured it to be. For example, by adjusting the sample rate to 4000Hz, it sounded normal and had the correct length.

        I believe the problem exists due to the hard-coded values in copyWaveFile():

        int channels = 2;
        long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels/8;

        There is only a single channel being sampled with mono audio, and byteRate ends up being twice what it should be. This can be corrected by setting int channels = 1 for mono sources.

        • Krishnaraj Varma March 6, 2011 / 9:39 am

          Thank you Kyle for pointing out the issue, I hope you issue is fixed now.

          I will update the source code and checkin soon.

        • T-Money December 30, 2011 / 9:35 pm

          For future reference, you also need to change the “block align” bit that’s being written out at header[32]. It should be (channels * RECORDER_BPP / 8).

    • hello world March 14, 2016 / 7:20 am

      Did you manage to solve the fast forwarded issue?

  3. Radzell February 5, 2011 / 1:48 am

    I been using this solution for a project. I am having two small problems one is that my voice seem distorted and i can figure out how to play the audio on my android device after i record it.

    • Krishnaraj Varma February 6, 2011 / 9:00 am

      Randzell, there could be so many reasons for the distorted voice, can you check weather the output file is in correct format (I assume it is a WAV file)?

      The file can be played by different desktop and mobile audio players including the default one in Nexus One.

  4. Gi February 9, 2011 / 5:06 pm

    Does anyone know how to turn off dynamic range compression ( I’m interested in processing raw signal, I mean – when there is a very quiet signal, I would like to record very low raw values. But I get a quiet signal amplified and the resulting raw value is a high value instead. Is it possible to turn this “feature” off? Or is it implemented in the hardware part without an “turn off” option?

  5. sudheer February 16, 2011 / 10:47 am

    Thanks for your reply. As the Audio Record class does support PCM 8 bit and 16 bit, Can we do our own encoding in other formats, especially I would like to format in ADPCM at 8Khz, Mono and 4 Bit. Is this possible in Android. Kindly help me if you have some ideas to do so.

    Kind Regards

  6. david sun March 19, 2011 / 8:19 am

    I am a beginner in android world, I just setup Eclipse, and I can run “hello android” with an emulator on my PC, this is my first Java app, this is fun. I’d like to try audiorecord.2 on my Eclipse environment, buy I have no idea how to setup a project linked to source code downloaded from SVN. could you please give me some hint( instruction) on how to build a Eclipse project with AudioRecoder2. any suggestions are appreciated. what I did is build a brand new project called audiorecoder, and copy sorce code to src directory, but when I run, it tells me “the public type RecorderActivity must be defined in its own file”, also it says” id can not be resolved or is not a field.”. “Applog can not be resolved”.

  7. david sun March 20, 2011 / 4:51 am

    Hi, after I play with Eclipse for a while, I can run audiorecorder2, the emulator pop up a window, “Sorry! The applicaion Audio Recorder(process com.varma.samples.audiorecorder)has stopped unexpectedly. please try again. “source not found”, after I open debug information, it shows ” the source attachment does not contain the source for the file AudioRecord.class. how to add AudioRecord.class to develop environment.

  8. david sun March 20, 2011 / 6:49 am

    my last question, and I think it is much easier to answer, could this Audiorecorder.1 or 2 run on Emulator? where will the data file be saved? I am using an IBM thinkpad T43. Windows XP.
    Eclipse Jave EE IDE for web developers, Version:Helios service release 2. build Id:20110218-0911

  9. TC March 21, 2011 / 10:39 am

    Thanks for the info. I have a follow up question… Now that we have a way to get raw PCM data. How would I be able to convert the PCM data to MPEG4 or 3GPP? Is there any API that allows me to feed it the PCM data file and it would spit out say 3GPP file?


    • Krishnaraj Varma March 27, 2011 / 5:52 pm

      To my knowledge there is no built-in APIs available for the compression PCM data, you have to use any libraries to convert the PCM to MPEG4 or 3GPP.

  10. Tom N April 28, 2011 / 5:02 pm

    Thanks for posting this code, very useful, though I had to go into the debugger to understand why it was crashing on my phone (the original Motorola Droid). It turns out that AudioRecord.getMinBufferSize() was returning AudioRecord.ERROR_BAD_VALUE (in this case because AudioFormat.CHANNEL_IN_STEREO is apparently not supported). You may want to add a check for this.
    Thanks – Tom

    • Krishnaraj Varma May 8, 2011 / 8:20 am

      Thank you Tom for pointing out the mistake, will correct the code and update the SVN.

  11. Mayu September 1, 2011 / 1:42 pm

    I’ve used MediaRecorder class previously and found out that I can’t use pause method. Now I tried with your example to do AudioRecorder class too but couldn’t figure out how.

    could you please tell me how?? or some external resources??

    • Rathish June 8, 2012 / 9:30 am

      Hi Mayu,
      Did you found the solution for pausing an audio recording. IF u hav got the solution means plz reply me back. tis is my id. So plz reply back, if u got the solution.

  12. Leonardo September 2, 2011 / 11:10 am

    This post was very usefull, it really works good.. Congrats.
    I’ve a question, do you know how to pause the recording? May be a temporal file to contain the previous recorded sound and then must do a binding of files?

    • Rathish June 8, 2012 / 9:33 am

      Hi Mayu,
      Did you found the solution for pausing an audio recording. If u hav the solution means plz reply me back. tis is my id. So plz reply back, if u got the solution.

  13. PenguinMan October 31, 2011 / 12:20 pm

    It looks good.
    But can anyone point me to an example that will just work.
    I want to read raw samples from the mic.
    But this example has errors in Eclipse such as: “ can not be resolved as a field”
    And after a few hours of playing with similar examples, the error is “AudioFlinger could not create record track : -1”
    This is a fairly simple thing to want to do;.. read some audio samples…I have done it 20 years ago on a PC..I have programmed for 20 years !! including Java. Android audio recording, must be the most complicated and convoluted mission ever devised. Perhaps I am the first person ever to want to record some bytes from a microphone on an android device ?, is that such a spectacular thing to ask for ?
    What is wrong with you all ?

  14. PenguinMan October 31, 2011 / 12:49 pm

    I found the cause of my problem:

    This is the SECRET to getting audio recording to work if you are having problems starting audiorecord..
    The Manifest file needs the RECORD_AUDIO permission set

    That’s all !

    (It took me two days to work that out)

    (my last comment wasn’t helpful, if it was posted, please remove)

    • venu December 19, 2013 / 10:49 pm

      Thank you very much. I struggled for one full day, finally with your answer/tip my problem is resolved.

  15. Nishant Rajput November 4, 2011 / 5:41 am

    Hello Krishnaraj,

    Using Your Code m Converting Raw PCM Data to AAC using faac library through JNI. Its not difficult at all but m getting such humming sound in my converted AAC audio.

    Can you Work around it.? If u want i can send you my code.

    Nishant Rajput

  16. Jerome Doby February 5, 2012 / 3:29 am

    Mr. Varma,

    Thanks for your example of how to make the AudioRecord class work. I do have a problem, however when using your class. For some reason, the outputted WAV file has me sounding sped up, and also the pitch is that of a ‘chipmunk’. I have also done some conversion of the raw PCM file to MP3, without using the WAV headers, and it is the same result.

    I have read the AudioRecord API docs, and I can’t see anything that would resolve this problem.

    I’d be eternally grateful if you could help me figure out why this is happening.

    Thanks in advance.

    • Jerome Doby February 5, 2012 / 3:47 am

      Nevermind — it seems to be a problem with my rooted Vibrant running Ice Cream Sandwich. I tried on my wife’s unmodified phone, and it works just fine. Thanks for a great code example.

  17. sky_lo February 9, 2012 / 8:17 pm

    i click record…
    then i click stop…
    then nothing. how/where can i view the file?
    how can i play it?
    has a file even been recorded??? no errors or feedback of anykind.
    this is on my G2 device by the way… deplyed via: ‘adb install AudioRecorder.2/AudioRecorder.2.apk’

    when the G2 device is mounted, will i be able to navigate to the dir and view the file as well?


  18. sky_lo February 9, 2012 / 8:45 pm

    okay, i was able to view the wavs in the SDCard/AudioRecorder folder…
    haven’t yet been able to play them tho

  19. shai lavi February 13, 2012 / 6:07 pm

    Thanks for your code ‘helped me a lot solving the problem i had.
    wish u all the best

  20. gangnith February 17, 2012 / 9:31 am the link that you have put to download the source,.. i downloaded and executed..

    but i get a “force close” on my emulator.. and the error occurs on my “startRecording” function
    ,… are there are any pre-requisites for this ? can you pls help

  21. Wendy February 28, 2012 / 5:22 am

    Hi, this is really a great example, it helps me a lot, but how if i want to change pcm 16 bits to 8 bits?
    There is a buffer size error….
    thanks for ur helping!!

  22. Subair March 1, 2012 / 12:09 pm

    Clicking on the record button results in a crash !!

    java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
    at com.demo.audiorecorder.AudioRecorderActivity.startRecording(
    at com.demo.audiorecorder.AudioRecorderActivity.access$1(
    at com.demo.audiorecorder.AudioRecorderActivity$1.onClick(

  23. ravikanth March 30, 2012 / 6:17 am

    Hello Ajith !
    Your Auido recording article is very helpful. I have some requirements like, I want to record audio with PCM_16 bit encoding type and 8000 sample rate in stereo mode and then separate the two channels data into two individual buffers from the main buffer which has raw PCM data of stereo audio. Can you please help me put with this

  24. Sandeep April 30, 2012 / 5:11 am

    Hi, nice example.Really helping thans a lot.
    Could you suggest or give some code or resouce to have pause button as well.
    that will pause recording .

    Thanks a lot.

    • Madhu Reddy June 7, 2012 / 10:52 am

      Hi, Nice tutorial, i am able to record and playing back the recorded data. But How can i ensure like ‘there are no samples missing’ in my recording. i am having following issues

      1. difference between statRecording() and read() methods in functionality wise.

      2. For example i have started my recorder instance and by creating new thread i am reading samples using read() from device regularily. but in a case if i am not calling this read() regularly(since i am having other processing load) will i loose the data? and how this buffer pointer gets update?. what is the size difference to the recorder instance buffer and other audio buffer using in read().

      3. Why my recorder instance failing with 44.1 and 48k sampling rate?

      Thanks in Advance

  25. Ruchi June 13, 2012 / 11:35 am


    I want to implement pause and resume functionality while recording and playing using AndroidRecord.
    Kindly suggest how to do that. I am new to android programming.

    Thanks and regards.

  26. Ruchi June 14, 2012 / 6:16 am

    I executed the above souce code on Galaxy R(2.3.5 OS) and Sony Ericson (4.0 OS).
    My trouble is on Sony Ericson I can play the recorded file, but on Galaxy R I cant play it.
    The playing just finishes in less than a second on the later one.
    Please help.

  27. Murali June 19, 2012 / 1:03 pm

    It is nice example.Really helping thans a lot. The wav takes more size , so can you suggest me how to compress the wav file for reduce the size? please help me.

  28. Meena July 31, 2012 / 6:34 am

    I have created a file name “music_1.amr”.. When i recording audio it will capture and stored in “music_1.amr” . So i can do play, stop or save.. Now i wanna do is, whenever i create a new audio recording that previous recorded audio should be deleted automatically and this new audio capture must store it in same file name “music_1.amr”. What do i wanna do here . Can anyone suggest me?

  29. chinni August 31, 2012 / 1:06 pm

    Nice tutorial

  30. Phalanx September 26, 2012 / 7:54 am

    Hello ! Thanks for this code, very helpful !
    However, I still have a small problem with something and I can’t find any information about this : in the code, the sentence ” AppLog.logString” presents an error.

    AppLog is underlined in red with the explanation : AppLog cannot be resolved. The “help” menu gives me some option as usual “create variable, create filed, and so on…” but any of them implies that I will have to add some code for applog an I have no idea what to do!
    I can’t find anything about this by googling “applog”, this problem keeps me from doing any progress, so any help is highly expected!!

    Thanks in advance !

  31. Chris January 2, 2013 / 9:07 pm

    Guys, I’m new to this but I spent a lot of time getting it to compile by copying/pasting the files into an existing empty package.. Is there a way to download the entire project into Eclipse in a single operation?

    I finally got it to compile, but it dies immediately on the phone:

    01-02 21:02:46.146: E/AndroidRuntime(4898): Uncaught handler: thread main exiting due to uncaught exception
    01-02 21:02:46.156: E/AndroidRuntime(4898): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.noiserecorder/com.example.noiserecorder.RecorderActivity}: java.lang.ClassNotFoundException: com.example.noiserecorder.RecorderActivity in loader dalvik.system.PathClassLoader@2fb24cd0
    01-02 21:02:46.156: E/AndroidRuntime(4898): at
    01-02 21:02:46.156: E/AndroidRuntime(4898): at
    01-02 21:02:46.156: E/AndroidRuntime(4898): at$2200(
    01-02 21:02:46.156: E/AndroidRuntime(4898): at$H.handleMessage(
    01-02 21:02:46.156: E/AndroidRuntime(4898): at android.os.Handler.dispatchMessage(
    01-02 21:02:46.156: E/AndroidRuntime(4898): at android.os.Looper.loop(
    01-02 21:02:46.156: E/AndroidRuntime(4898): at
    01-02 21:02:46.156: E/AndroidRuntime(4898): at java.lang.reflect.Method.invokeNative(Native Method)
    01-02 21:02:46.156: E/AndroidRuntime(4898): at java.lang.reflect.Method.invoke(
    01-02 21:02:46.156: E/AndroidRuntime(4898): at$

    Any help would be greatly appreciated.


  32. Chandan February 27, 2013 / 8:57 am

    It is a nice tutorial. The code is working fine. The information on how to write mp3 and mp4 file using your code and on how to compress these file in mp3 or some other format will increase the beauty and completeness of this tutorial.


  33. Toleen March 2, 2013 / 10:04 am


    I tried to integrate the recording function within a service, but the problem is that the conversion from raw to wav does not work perfectly.if the recording is more than 10 min the raw file is not converted completely.
    do u have any idea what would be the reason?


    • Deo October 30, 2014 / 5:40 pm

      Hello TOLEEN!
      Did you get the solution. I have same problem!

  34. Carena Manznao April 4, 2013 / 6:14 am

    hi ..can i ask question?
    How will you rename the file name. I mean file name for example should be: track 1 and so on after you have another recording.
    Please help me. thank you

  35. Anil April 19, 2013 / 5:09 am

    Sir I want to know how start recording when I speak and stop recording when stop speaking
    Just like “Talking tom cat”…
    I hope you will help me…

  36. Omkar May 16, 2013 / 1:47 pm

    Hi there,
    I’m trying to make an app that controls the brightness of the screen through a plug-in device which can be connected to audio jack of mobile device.
    Any idea about that??

  37. Eman June 2, 2013 / 3:46 pm

    hello,,, thank you so much for this article,,, but i just have one question… where is the code?! :O

  38. jessy June 28, 2013 / 7:22 am

    Hi,Thanks, a lot, but I have a question please, when I try to listen to what I have recorded , I heard nothing but a distorted sound and also the wave sample length is zero seconds although I recorded a sample of 8 scnds, Can anyone please help,

  39. Amit Gauraw January 15, 2014 / 9:32 am

    Hi ,

    I have to sream mic input over the http. Can you please help me how to achieve this task.

    Thanks in advance.

  40. RKD August 28, 2014 / 12:17 pm

    Thank u so much!! I executed it at my first attempt! After browsing lots of sites, finally I got your link and it works perfectly.

  41. Deo October 30, 2014 / 5:38 pm

    Hey! How to download Sample Project? It just shows src code there..

  42. Prahalad June 9, 2016 / 6:54 pm

    When i am using your code than in Samsung s7, only raw file are created and not playing

Leave a Reply

Your email address will not be published. Required fields are marked *