RSS Entries RSS
RSS Subscribe by Email

Android Audio Recording Tutorial

After many hours of trying to record audio on my Google Android device, I’ve finally arrived at a workable solution.  There were a few bumps along the way besides the horribly out of date MediaRecorder documentation, which was sorely lacking details.  For one, I could only get audio to record to the SD card.  Additionally, the directory being recorded to must already exist before attempting to record to it. Without further ado, here is a complete example for recording audio on the Android via the MediaRecorder API:


package com.benmccann.android.hello;

import java.io.File;
import java.io.IOException;

import android.media.MediaRecorder;
import android.os.Environment;

/**
 * @author <a href="http://www.benmccann.com">Ben McCann</a>
 */
public class AudioRecorder {

  final MediaRecorder recorder = new MediaRecorder();
  final String path;

  /**
   * Creates a new audio recording at the given path (relative to root of SD card).
   */
  public AudioRecorder(String path) {
    this.path = sanitizePath(path);
  }

  private String sanitizePath(String path) {
    if (!path.startsWith("/")) {
      path = "/" + path;
    }
    if (!path.contains(".")) {
      path += ".3gp";
    }
    return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
  }

  /**
   * Starts a new recording.
   */
  public void start() throws IOException {
    String state = android.os.Environment.getExternalStorageState();
    if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  {
        throw new IOException("SD Card is not mounted.  It is " + state + ".");
    }

    // make sure the directory we plan to store the recording in exists
    File directory = new File(path).getParentFile();
    if (!directory.exists() && !directory.mkdirs()) {
      throw new IOException("Path to file could not be created.");
    }

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(path);
    recorder.prepare();
    recorder.start();
  }

  /**
   * Stops a recording that has been previously started.
   */
  public void stop() throws IOException {
    recorder.stop();
    recorder.release();
  }

}
Share and Enjoy:
  • Add to favorites
  • HackerNews
  • DZone
  • Reddit
  • del.icio.us
  • StumbleUpon
  • Slashdot
  • Digg
  • Google Bookmarks
  • Facebook

Tags:

87 Comments »

  1. dragonksn said,

    April 1, 2009 at 9:30 pm

    Hello, I’m a new comer to Android and I am a bit poor in Java.
    I want to record a sound in my Android Emulator, so I copied your code(change the package name a bit) and Run. But it says “The Application Android, Application(process AndroidTest.package) has stopped unexpectedly. Please try again”. “Android, Application” is my app name and “AndroidTest.package” is my package name.

    Can u tell me why it can’t be run? I’ve tried to change the code to make it run, but I can’t. Please help me! I must be able to get a audio stream into Android to test with my other urgent end year project.

    Thanks in advanced :)

  2. david said,

    April 2, 2009 at 11:06 pm

    hi Ben, I encountered the same problem on G1 as dragonksn’s when using your code . “The application audiorecorder(process com.david.android.audiorecorder) has stopped unexpectedly. Please try again”.

    My audiorecoder class looks like as:

    import android.app.Activity;
    import android.os.Bundle;
    //importing yours

    public class audiorecorder extends Activity {
    //then following your codes

    }

    Below is the Logcat info:

    D/AndroidRuntime( 815): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
    D/AndroidRuntime( 815): CheckJNI is ON
    D/AndroidRuntime( 815): — registering native functions —
    I/jdwp ( 815): received file descriptor 25 from ADB
    I/ActivityManager( 56): Starting activity: Intent { flags=0×10000000 comp={com.david.android.audiorecorder/com.david.android.audiorecorder.audiorecorder} }
    I/ActivityManager( 56): Start proc com.david.android.audiorecorder for activity com.david.android.audiorecorder/.audiorecorder: pid=824 uid=10040 gids={}
    D/AndroidRuntime( 815): Shutting down VM
    D/dalvikvm( 815): DestroyJavaVM waiting for non-daemon threads to exit
    D/dalvikvm( 815): DestroyJavaVM shutting VM down
    D/dalvikvm( 815): HeapWorker thread shutting down
    D/dalvikvm( 815): HeapWorker thread has shut down
    D/jdwp ( 815): JDWP shutting down net…
    D/jdwp ( 815): +++ peer disconnected
    I/dalvikvm( 815): Debugger has detached; object registry had 1 entries
    D/dalvikvm( 815): VM cleaning up
    D/dalvikvm( 815): LinearAlloc 0×0 used 594756 of 4194304 (14%)
    I/jdwp ( 824): received file descriptor 10 from ADB
    D/dalvikvm( 824): newInstance failed: no ()
    D/AndroidRuntime( 824): Shutting down VM
    W/dalvikvm( 824): threadid=3: thread exiting with uncaught exception (group=0x4000fe68)
    E/AndroidRuntime( 824): Uncaught handler: thread main exiting due to uncaught exception
    E/AndroidRuntime( 824): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.david.android.audiorecorder/com.david.android.audiorecorder.audiorecorder}: java.lang.InstantiationException: com.david.android.audiorecorder.audiorecorder
    E/AndroidRuntime( 824): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2082)
    E/AndroidRuntime( 824): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2172)
    E/AndroidRuntime( 824): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
    E/AndroidRuntime( 824): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1586)
    E/AndroidRuntime( 824): at android.os.Handler.dispatchMessage(Handler.java:99)
    E/AndroidRuntime( 824): at android.os.Looper.loop(Looper.java:123)
    E/AndroidRuntime( 824): at android.app.ActivityThread.main(ActivityThread.java:3790)
    E/AndroidRuntime( 824): at java.lang.reflect.Method.invokeNative(Native Method)
    E/AndroidRuntime( 824): at java.lang.reflect.Method.invoke(Method.java:521)
    E/AndroidRuntime( 824): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
    E/AndroidRuntime( 824): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:503)
    E/AndroidRuntime( 824): at dalvik.system.NativeStart.main(Native Method)
    E/AndroidRuntime( 824): Caused by: java.lang.InstantiationException: com.david.android.audiorecorder.audiorecorder
    E/AndroidRuntime( 824): at java.lang.Class.newInstanceImpl(Native Method)
    E/AndroidRuntime( 824): at java.lang.Class.newInstance(Class.java:1458)
    E/AndroidRuntime( 824): at android.app.Instrumentation.newActivity(Instrumentation.java:1097)
    E/AndroidRuntime( 824): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
    E/AndroidRuntime( 824): … 11 more
    I/Process ( 56): Sending signal. PID: 824 SIG: 3
    I/dalvikvm( 824): threadid=7: reacting to signal 3
    I/dalvikvm( 824): Wrote stack trace to ‘/data/anr/traces.txt’
    W/InputManagerService( 56): Starting input on non-focused client android.view.inputmethod.InputMethodManager$1@43704540 (uid=1000 pid=56)
    W/InputManagerService( 56): Ignoring focus gain of: android.view.inputmethod.InputMethodManager$1@43704540
    W/ActivityManager( 56): Launch timeout has expired, giving up wake lock!
    W/ActivityManager( 56): Activity idle timeout for HistoryRecord{43852e68 {com.david.android.audiorecorder/com.david.android.audiorecorder.audiorecorder}}
    D/dalvikvm( 100): GC freed 1763 objects / 93784 bytes in 135ms
    I/Process ( 824): Sending signal. PID: 824 SIG: 9
    I/ActivityManager( 56): Process com.david.android.audiorecorder (pid 824) has died.
    D/InputManagerService( 56): hide the small icon for the input method
    V/ActivityThread( 100): Resuming ActivityRecord{436cc620 token=android.os.BinderProxy@436cc0b0 {com.android.launcher/com.android.launcher.Launcher}} with isForward=false

    I’m sorry for the mess. Could your please help me figure out what’s the reason caused this issue? Thanks a lot.

    david

  3. krtek said,

    April 4, 2009 at 9:16 am

    it works, thanks :-)

    btw. did you try to call stop() and then start() again? I did and application gets closed without a word (e.g. no exception) :-( (

  4. krtek said,

    April 4, 2009 at 9:24 am

    dragonksn, david:

    Hi, the cause is that the source above is not an android *application*, just a helper class. So it just can’t run by itself.

    You have to do something like this:

    public class MyActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final AudioRecorder recorder = new AudioRecorder(“/audiometer/temp”);
    recorder.start();

    //….wait a while
    recorder.stop();
    }
    }

  5. Ben said,

    April 4, 2009 at 10:51 am

    krtek, my stop method calls release, which is why you can’t call stop and then start again. I believe if you take out the call to release you will be able to call start again.

  6. david said,

    April 6, 2009 at 8:16 pm

    Great! Now it works. Thanks to krtek & ben, you’re all good-heart men -_-

    david

  7. dragonksn said,

    April 6, 2009 at 9:23 pm

    Thanks for help krtek, but still I have problem :(
    Sorry first that ask simple question but really I can’t find solution for that. I’m fool now :( .

    I continue with your code
    final AudioRecorder recorder = new AudioRecorder(”/audiometer/temp”);

    but it says the constructor is undefined (at the part) even I have already create a new classe of AudioRecorder(copied your code). When I mouse over audiometer, it says “audiometer can not be resolved”. And when I mouse over temp, it says “The method temp() is undefined for the type AndroidName”.

    I tried with my SDCARD image folder “/media/Storage/Datas/I5_2008_2009/stage/MICA/support/Software/Android/Android_sdk/android-sdk-linux_x86-1.0_r2/tools/myandroid/sdcard.img” and the same thing happens(the same error messeges).

    I also tried to insert sleep(6000); to waiting phase. Then it ask for sleep method. Can u tell why it ask for sleep method, as I saw that sleep method is already a public method, can’t I use it directely?

    Thanks in advanced for help :)

  8. dragonksn said,

    April 7, 2009 at 1:26 am

    I now found the problem with
    final AudioRecorder recorder = new AudioRecorder(”/audiometer/temp”);
    It has problem because the double quoat is not standard(the correct one), just rewrite it, then it work :)

    But now I want to ask u one more question:
    I use the code to record but there isn’t any file created in my SDCARD image. So is my path incorrect?
    (my SDCARD what I used in “run configuration”, where it already work with camera and my music files)

    Can anyone help me please?
    *I ask so often because I want to solve the problem as quick as possible, sorry in advanced :| I try my best to solve the problem before asking :)

  9. dragonksn said,

    April 9, 2009 at 9:04 pm

    Hi, Can anyone help me to play audio stream directly from Microphone in real time? I want this because I want to do voice synthesis in real time.
    Thanks in advanced, :)

    dragonksn

  10. Ben said,

    April 9, 2009 at 11:21 pm

    Android does not currently support streaming of audio directly. You could probably tail the file as it’s being written though.

  11. dragonksn said,

    April 15, 2009 at 7:01 pm

    Hi Ben,

    I’ve tried many ways for many days to tail the stream but I can’t do that. Can u give me some idea or help me with it?
    Thanks for your kind help :)

    dragonksn

  12. Ben said,

    April 15, 2009 at 7:33 pm

    I haven’t actually tailed the stream myself, but saw someone mentioning it on the Android developer’s list. You might want to do a search there. I personally am just going to wait until the 1.5 SDK comes out since it sounds like it will have better support for that type of thing.

  13. dragonksn said,

    April 15, 2009 at 7:48 pm

    In addition, I think there might be problem with the mounting of the SDCARD as the new recorded file can be played only when I rerun the emulator. If not, there is no way to see the new recorded file. So is there anymeans to play the newly recorded file or is there other way to tail that file? Or is there any temp file for the recording file so that I can play without restarting the emulator??? (as I’ve tested many time, I think there is temp file but I can’t find where it is and i don’t know if I can play it)

    Thx :)
    dragonksn

  14. Ben said,

    April 15, 2009 at 7:54 pm

    You can used adb (the Android Debug Bridge) to copy files off the emulator: http://developer.android.com/guide/developing/tools/adb.html

  15. dragonksn said,

    April 15, 2009 at 8:04 pm

    What about the temp file of the Android emulator, is there any as what I thought? And where it is?

  16. dragonksn said,

    April 15, 2009 at 9:01 pm

    But copying file to local machine is not really a good idea for me. Cause my app must work directly in the android individually. Can u tell me where does the temp file of android is (if there is as what I’ve thought)? And is it possible to play that???
    Thx u so much for help :)
    dragonksn

  17. nicky said,

    April 17, 2009 at 8:12 am

    I have a working app for streaming audio that work with android sdk 1.5 but the quality is very poor and there is a 5-10 second delay. If anyone is interested let me know.

  18. dragonksn said,

    April 28, 2009 at 11:22 am

    sure, I’m very interested in your work. Can u let me know how you do it in detail?
    Thx,
    dragon

  19. nilesh said,

    May 7, 2009 at 10:26 pm

    I am also interested. Please let me know.

  20. sri said,

    May 14, 2009 at 2:02 am

    hi, the emulator has comes to screen and display “Hello World, Audio Recorder!”, but i am not able to seen any recording device and no audio file has created in hard disk. should create the “temp” folder or what is the problem? Thanks in advance

  21. sri said,

    May 14, 2009 at 4:30 am

    Hi, Its working perfect in my phone , Great work.

  22. vineeth said,

    May 24, 2009 at 11:12 pm

    Please send me the audio recording code for android simulator. I tried the above code. Its not working. Please send me the code for audio recording.

  23. ashutosh said,

    May 26, 2009 at 8:13 pm

    Can you provide similar example for Video record/capture too?

  24. thenewbie said,

    July 7, 2009 at 4:58 am

    Wow thanks man! works perfect!

  25. Sisi said,

    July 28, 2009 at 6:43 am

    Hello,
    i try this application, but it’s not playing.
    I think my path is not good… but i don’t know how i have to change!
    Here is my code..it’s a little bit long, because i also use Button. I can’t playing what i have recording!

    package com.swyx.AudioRecording2;

    import java.io.IOException;

    import android.app.Activity;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;

    public class Record extends Activity {
    //Wahrscheinlich ist den angegebenen Pfad nicht richtig! Nur wie gebe ich richtig ein?

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
    //System.out.print(“bis hier geht”);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /*Erstelle ein neues Objekt von Typ Button,
    das aktiviert wird sobald es geklickt wird*/

    Button btn1 = (Button)findViewById(R.id.record);
    btn1.setOnClickListener(btnListener1);

    Button btn2 = (Button)findViewById(R.id.stop);
    btn2.setOnClickListener(btnListener2);

    Button btn3 = (Button)findViewById(R.id.play);
    btn3.setOnClickListener(btnListener3);

    }
    //folgendes passiert, wenn auf btn1 geklickt wird

    private OnClickListener btnListener1 = new OnClickListener()
    {
    public void onClick(View v)
    {

    final AudioRecorder2 recorder = new AudioRecorder2(“C:/Bochum”);
    try {

    recorder.start();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    //….wait a while
    try {
    recorder.stop();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }

    };

    //folgendes passiert, wenn auf btn2 geklickt wird

    private OnClickListener btnListener2 = new OnClickListener()
    {
    public void onClick(View v)
    {
    MediaPlayer mp = new MediaPlayer();
    Toast.makeText(getBaseContext(),
    “Hello, please stop!” + mp.isPlaying(),
    Toast.LENGTH_LONG).show();
    /*
    try {
    recorder.stop();

    } catch (IOException e) {

    }*/
    }
    };

    //folgendes passiert, wenn auf btn3 geklickt wird

    private OnClickListener btnListener3 = new OnClickListener()
    {
    public void onClick(View v)
    {

    MediaPlayer mp = new MediaPlayer();

    try {
    mp.setDataSource(“C:/Bochum”);
    } catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    mp.prepare();
    } catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    mp.start();
    Toast.makeText(getBaseContext(),
    “Hello, please play!” +” is playing?: “+ mp.isPlaying(),
    Toast.LENGTH_LONG).show();
    }
    };
    }

    Thanks for your help! I’m desesperate!
    Sisi

  26. Sisi said,

    July 28, 2009 at 6:45 am

    PS: I have also copy your Class in the same Project!

  27. Ben said,

    July 28, 2009 at 9:39 am

    Sisi,
    You can’t record to C:\ because that’s a Windows path. Android uses Linux type paths. Try recording to something like “test/tmp.3gp” instead.

  28. Sisi said,

    July 29, 2009 at 12:10 am

    Thank you Ben!
    Can i also record or play .mp3?
    It’s difficult for me because i never use android bevor.
    Thanks a lot for your help
    Sisi

  29. Sisi said,

    July 29, 2009 at 1:27 am

    It works! i have a file .3gp.
    I now want to try, if i can play what i have record with my microphone!
    I use also emulator terminal to see which file or directory i have!

  30. Sisi said,

    July 29, 2009 at 5:51 am

    yes it works! i can also play .mp3! .3gp is only for video, isn’t it?

  31. Ben said,

    July 29, 2009 at 9:25 am

    Sisi,
    .3gp is also the format for Android audio. It’s a bit confusing, but 3gp is a container, which means that it holds the true format inside of it. In the case of Android audio, it’s actually an AMR-NB file inside of a 3gp container.

  32. Manoj said,

    August 1, 2009 at 1:14 am

    Hi, Benn,

    can we able to record the audio using aac encoding? if so how? if not is there any way to do like that?

  33. Ben said,

    August 1, 2009 at 5:22 pm

    Hi Manoj,
    Android currently only supports recording in AMR-NB in a 3gp container. The Android kernel would have to be patched in native C code to support recording in AAC.

  34. ia said,

    August 11, 2009 at 1:06 am

    hey all

    anybody know how to add the phone numbers that takes a part in the call and the date of the call into the recording name?

    for example a call between phone number 0541111111 and phone number 054222222 that happaend in 2.3.2009

    will be save like this 0541111111_054222222_2.3.2009.3gp

  35. Oleksandr said,

    August 18, 2009 at 1:09 am

    Hello Ben,

    Could you help me manage audio latency. I use AudioRecorder and when I measure latency using System.currentTimeMillis() and get record + playback = 50 ms
    but when I measure the same using PC mic + PC speaker with test signal I got 320ms

    Could you suggest the solution or even explain what is the problem

    Thanks
    Oleksandr

  36. Ben said,

    August 19, 2009 at 2:13 pm

    Hi Oleksandr,
    Not sure I quite understand the problem. You are upset Android is too fast? :o ) When you say using the PC is that directly on the PC or is that the Android emulator?

  37. ia said,

    August 24, 2009 at 12:34 am

    Hi Ben
    can answer my question that i ask…. (:

    Thanks Itay

  38. gunnar-medial said,

    September 12, 2009 at 3:05 pm

    Thanks a lot for these writings.
    It is going to help me a lot in realising my voice solution.

  39. Senthil.M said,

    September 14, 2009 at 6:03 am

    Hi Ben,

    I am using the above code to record the audio in sdcard. i can get the file named temp in sdcard with the recorded content. How can i play it in android emulator.. I am trying to play it through MediaPlayer when i click a button. But it shows the following error in logcat .

    ERROR/PlayerDriver(554): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported
    ERROR/MediaPlayer(19985): error (1, -4)
    WARN/System.err(19985): java.io.IOException: Prepare failed.: status=0×1

    I don know why i am getting like this.. How to rectify this problem.. Can you please provide the solution for this..?

    thanks in advance,
    Senthil.M

  40. mugdha said,

    October 3, 2009 at 6:37 pm

    You guys are not able to record sound in emulator because the android emulator doesn’t support it yet. This code should only work on the phone.

  41. Dave said,

    October 5, 2009 at 7:56 am

    Ben,

    Thanks for the heads up. This got it working for me. Why don’t they state the file needs to be there before recording? Sheesh.

    -Dave

  42. deepa said,

    October 12, 2009 at 9:33 pm

    hi, im new to this android application but i need to port sound recorder application on beagle board .. if anybody can provide me the link for where i can download its .apk …

  43. Mahbub said,

    October 19, 2009 at 2:20 am

    Hello Everyone,

    I am new in Android development and using SDK 1.5. I have tried this code to record audio. But I found “W/ServiceManager( 35): Permission failure: android.permission.RECORD_AUDIO” in logcat.

    How can I overcome this problem?

    Thanks in advance

  44. Mahbub said,

    October 19, 2009 at 2:53 am

    Hello everyone

    The problem is solved with the change in the permission of the manifest file….

    Thanks Ben for the nice code.

  45. Moritz said,

    October 21, 2009 at 7:55 pm

    Hi!

    I am more of a perl coder than a java nut, so I’m a bit stumped on how to compile all this.

    Is there any chance that you or one of your readers could provide a precompiled .apk for this functionality?

    I’ve searched around, and couldn’t find any other app that works….

    Cheers,

    M.

  46. Ben said,

    October 22, 2009 at 3:36 pm

    Hi Moritz,
    This really isn’t a complete application, but only an example of how to create one. I hope it helps someone out there create a better sound recording app, but I’m afraid this alone isn’t going to be much help for you.

  47. Mahbub said,

    October 29, 2009 at 8:13 pm

    Hello everyone,

    Again thanks for the great effort here.

    I am trying to capture the raw sound data from the microphone of the device rather than saving it as .amr file. So that I can process it further by applying the FFT. is there any way to record the raw data using MediaRecorder class?

    need some information about the it?

    thanks in advance

  48. mahbub said,

    November 4, 2009 at 4:17 pm

    Hello Ben,

    Would please also help me to record the audio on PCM format directly from the MIC of the device. I am getting problem in the thread. Actually, I want to run a service in background to record audio continuously then it can be processed on other thread.

    Thanks in advance.

    Mahbub

  49. android_dev said,

    November 17, 2009 at 4:02 am

    Hi,
    i am trying to record and save video.My code is not working, i am definately making some mistake.can some please give me sample code to record and save video. i tried the above example.

  50. Piyush said,

    December 22, 2009 at 11:24 pm

    hey,Ben
    can you please help in recording Video with Android….

  51. Ben said,

    December 30, 2009 at 5:20 pm

    Hi Piyush, I’ve never recorded video on Android, so I’m afraid I can’t be of much help. Hopefully it’s not too different. Best of luck! -Ben

  52. Pitrsonek said,

    January 2, 2010 at 3:51 pm

    Hi is any idea how stream audio recording on server? You speak and behaind all what you say save to buffer and stream on server.

    I dont know how save audio from mic to buffer..

    THX

  53. Madlen said,

    January 12, 2010 at 5:42 am

    Hello Ben .

    i am trying to record and save voice.My code is not working, i am definately making some mistake.can some please give me sample code to record i tried the above example.
    Please help.

  54. decode said,

    January 15, 2010 at 5:11 pm

    hi ben,

    how can someone receive a buffer of audio data once its recorded and is ready to be written to file or stream or something else. is there any callback by the mediarecorder or a notification? so that the code can access that buffer and can go ahead and use it.

    appreciate your response and time.

    thanks
    pk

  55. Deepa said,

    January 24, 2010 at 12:11 pm

    Can anyone tell me what sensors can be used in the emulator with SDK 2.0 and can audio recording be done in the Android emulator?

  56. Ben said,

    January 24, 2010 at 8:20 pm

    Hi Deepa, you can record audio on the Android emulator.

  57. Hans said,

    January 25, 2010 at 2:48 pm

    The example does not work for me. This is the problem I’m experiencing:
    http://code.google.com/p/android/issues/detail?id=5063

    Has anyone also encountered this, and been able to find a workaround? SDK 2.1 and Nexus One.

  58. Acer said,

    February 2, 2010 at 11:19 am

    Hello,

    I’m a beginner in Android and request any help on the following.

    I’m trying to use an SDCard loaded on to the Emulator and store files
    programmatically.

    The code snippet is as below

    public class Downloader {
    public Downloader(String path) {
    this.path=sanitize(path);
    Log.d(“DEBUG”,”The File Path is ” + path);
    }

    public String sanitize(String path) {
    if (!path.startsWith(“/”)) {
    path = “/” + path;
    }

    return Environment.getExternalStorageDirectory().getAbsolutePath()
    +path;
    }

    public void start() throws IOException {
    // Check if Media is mounted else throw IOException
    String state = Environment.getExternalStorageState();
    if(state != Environment.MEDIA_MOUNTED) {
    Log.d(“DEBUG”, “The Card is Not Mounted ” + state +”–”);
    throw new IOException (“SD Card is not Mounted. It is in ” + state);
    }

    }

    With the above code I’m able to see the “The Card is Not Mounted –”
    message in the logcat. I’m making use of the following permissions in
    the manifest.xml

    Questions
    1. Are there additional permissions to be included?
    2. The Downloader.java is a helper class whose object is being used in
    a Receiver. Does giving the receiver the WRITE_EXTERNAL_STORAGE would
    also enable Downloader.java to access the SDCard
    3. Any other suggestions

    Best Regards
    Acer

  59. Mark said,

    February 9, 2010 at 7:03 am

    I have recorded several files on my android using the voice recorder, but I can not figure out how to ge t them off my card or convert them to a .wav file. Could you provide some help? Thanks.

  60. man910 said,

    February 12, 2010 at 11:12 am

    You can get the files off by using adb. Find out where the file is by executing “adb shell ls /sdcard” and then executing
    “adb pull “.

  61. Matias de la Vega said,

    February 23, 2010 at 6:53 am

    Fellow developers, for those having trouble with this MediaRecorder example, or any MediaRecorder sample code in the web (a lot of developers are facing the same issue and nobody answers), I recommend trying adding WRITE_EXTERNAL_STORAGE permission to your Android Manifest file, in the case that you are trying to record media to your external SDCARD. Adding the following line should do the trick. Let me know if it worked.

  62. param4android said,

    February 24, 2010 at 5:17 am

    Thank You Ben…
    and very much thank you too Matias de la Vega …..
    you solved a great problem

  63. param4android said,

    February 24, 2010 at 5:22 am

    now my code is running very well and i m able to record and play the audio from emulator…
    now wat about video?, i know dat emulator does not support video recording…but is there any trick?what is the way for a developer for checking out if his code fr recording a video is working properly ….I m not having real android device…only emulator….

  64. John Polo said,

    February 27, 2010 at 3:35 am

    Where does an Android emulator capture input audio from and output to when testing recording and playback? Also, for param4android, did you test with Linux or Windows Android emulator? Thanks.

  65. John Polo said,

    February 27, 2010 at 4:17 am

    Following Ben’s code and comments in this RSS feed, we were able to write an audio recorder and player for an Android emulator on Windows. However, on playback we found the recorded audio is at half speed of the input audio, with correct pitch but with noise. Since many in this feed reported successes using an emulator to record and play audio, may we ask how the emulators are configured, which version and on what platform? Thanks.

  66. Pushpa said,

    March 17, 2010 at 5:45 am

    Hi Ben,

    can you send me a code which can connect to remote server and read the response from the server.I have one project.it is able to write to socket but cant read from server socket.If u can send me your email id i will send it to you because there is no attachment option over here

    Thanks much

  67. androidev said,

    March 17, 2010 at 11:49 pm

    Can anybody tell where exactly the file get stored. I cant see any file.
    i am getting following n error when I try to set recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);

    ERROR/audio_input(52): unsupported parameter: x-pvmf/media-input-node/cap-config-interface;valtype=key_specific_value
    ERROR/audio_input(52): VerifyAndSetParameter failed
    ERROR/PVOMXEncNode(52): PVMFOMXEncNode-Audio_AMRNB::DoPrepare(): Got Component OMX.PV.amrencnb handle

    Any one can tell me why its happening

  68. srinu said,

    March 20, 2010 at 7:42 am

    but it shows audio source not found how to configure system MIC to the android application

  69. Anupam said,

    March 21, 2010 at 6:30 am

    Hi

    Has anyone had any success with setting the AudioSource to VOICE_CALL? It seems to only record one side of the conversation.

    Really really need help here.

    Thanks

  70. Shan said,

    March 28, 2010 at 11:26 am

    hi SISI how u doing? i have copied ur whole above code but btnlistener1 is working but the below code has lots of sysntax mistake. i want to integrate this whole class with main class how can i do dis please help me.

    //folgendes passiert, wenn auf btn2 geklickt wird

    private OnClickListener btnListener2 = new OnClickListener()
    {
    public void onClick(View v)
    {
    MediaPlayer mp = new MediaPlayer();
    Toast.makeText(getBaseContext(),
    “Hello, please stop!” + mp.isPlaying(),
    Toast.LENGTH_LONG).show();
    /*
    try {
    recorder.stop();

    } catch (IOException e) {

    }*/
    }
    };

    //folgendes passiert, wenn auf btn3 geklickt wird

    private OnClickListener btnListener3 = new OnClickListener()
    {
    public void onClick(View v)
    {

    MediaPlayer mp = new MediaPlayer();

    try {
    mp.setDataSource(“C:/Bochum”);
    } catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    mp.prepare();
    } catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    mp.start();
    Toast.makeText(getBaseContext(),
    “Hello, please play!” +” is playing?: “+ mp.isPlaying(),
    Toast.LENGTH_LONG).show();
    }
    };
    }

  71. Shan said,

    March 29, 2010 at 3:20 am

    SISI your code is error free but can u please tell me that u have created an object of AUDIORECORDER2 class which is described by the author. now can u please tell me that how did u run the programme. actually i am using 3 classes
    1- iSnore.java: this file contains the UI like input name, time setting e.t.c. this class has a button. now i want to create an event like if i click the DONE button than recording starts.
    2-AudioRecorder2.java: this class is same as the 1st code of this blog.
    3-Record.java: this class contains your code.

    now i want to call class number 3 on the button DONE of class 1. How can i do this.Please help me.

  72. Crystal said,

    April 24, 2010 at 12:15 am

    I tried your code, it kind of working for me. It can create a .3gp file, but I can’t open the file after download it to PC.
    Seems it just create a file with name contains “.3gp”, but it’s not a legal 3gp file.

  73. burningwire said,

    May 3, 2010 at 5:14 am

    hi i have enabled soundrecorder in android eclair. With sd card as the storage media, i am able to record and the file is also getting created as .3gp but i am not able to playback the recorded file.

  74. ipaelo said,

    May 10, 2010 at 6:17 am

    Thanks.

    It works.

  75. Clint said,

    May 17, 2010 at 11:18 am

    Hi. I have your helper class http://pastebin.com/JVzgmGCR
    and my activity http://pastebin.com/Vs2dAaFv
    For whatever reason the .start() and .stop() of your helper class just make the app crash every time.

  76. Clint said,

    May 18, 2010 at 9:04 am

    Adding the permission which I didnt know was needed, fixed the .start()
    But the .stop() is still making the application crash. The file is being created in the /sdcard/test directory but is 0 seconds long. I figure this has to do with not stopping and releasing the stream.

    Does anyone know why the method would cause a crash?
    public void stop() throws IOException {
    recorder.stop();
    recorder.release();
    }

  77. Clint said,

    May 21, 2010 at 8:11 am

    I now have the phone recording and not crashing on the start stop or release. But when I check the file on the card it still comes out as 0 seconds long. Im giving it time and speaking into it just to be sure. Is there any piece I’m missing in making the file store what is supposedly recording?

  78. Invenco said,

    May 25, 2010 at 7:40 am

    Hello Ben:

    I saw two men ask the same question but you as well as others didn’t answer it. I really want to kown the response.I play the audio in the emulator which is just recorded but it is half speed. And the quality is terrible. Why ? How to deal with it.

    Hope to receive your response.

  79. Clint said,

    May 27, 2010 at 1:48 pm

    My program started working fine and now records and plays it back all within the same activity. Not sure what I did to fix it if anything.

  80. al3x said,

    June 9, 2010 at 1:34 pm

    Hey Ben,
    first thanks for your great tutorial….it does work :D I guess I don’t tell anything new.

    Is it possible to set the AudioSource to the Speaker??? So I could record what i’m playing at the same time??

    Hope u got a quick answere for me :)

    Thanks again

  81. Namra said,

    June 10, 2010 at 1:23 am

    Great tutorial!
    but please can anyone give the complete code along with the xml code, please!

  82. vibs said,

    June 21, 2010 at 4:44 am

    Hello, can anybody paste the Entire code (along with MainActivity , xmls , and Android Manifest )… ?
    Instead of writing in broken format write it in entire!

  83. Jonah said,

    June 30, 2010 at 4:53 pm

    @vibs, this isn’t broken format. What you are asking is for someone to make an app for you so that you can take the entire source code and put your name on it. Not very fair if you ask me!

    Why don’t you try posting something helpful before asking others to write an entire app for you? Come on!

  84. Nilesh said,

    July 17, 2010 at 2:50 am

    Hi All,

    I am also facing the same problem as Invenco has mentioned. The quality of the sound recorded using MediaRecorder as given in the code by Ben is really terrible. Also, the speed is low.

    Does anybody know the solution to this problem?

    Regards,
    Nilesh

  85. clair said,

    July 20, 2010 at 12:26 am

    Hi, I have one question.
    Is that possible to pause or resume the recording on the eclair or froyo?
    As I checked, there is no API to pause or resume the recording in the android framework.
    But I found an application to support the pause/resume on the eclair.
    When checking the logs, it seems not to use the opencore PV layer. It was different with the native sound recorder logs. That means there’s way to pause or resume in the android platfom (withoug using the opencore????)….
    Does anybody know how to pause or resume the recording?
    Sincerely,
    Clair

  86. kenn said,

    July 28, 2010 at 12:37 am

    Hello Ben (or Anyone),

    Thanks for the tutorial! I was able to run it.. It works well..

    Does anyone have any idea if I want to store the recorded audio in internal memory?

    Thanks in advance..

  87. Asma-ull-Hosna said,

    August 29, 2010 at 9:07 pm

    Hi,
    How can I measure the start,stop,duration and pause time with progress bar for different video which is streaming from website?

RSS feed for comments on this post · TrackBack URL

Leave a Comment