Search code examples
androidandroid-emulatorandroid-widget

How to record audio file in Android


I am working in android. How can I record an audio file through microphone, and how can I save the recorded file in the emulator?


Solution

  • It is easy to record audio in Android. What you need to do is:

    1) Create the object for media record class : MediaRecorder recorder = new MediaRecorder();

    2) In the emulator, you're unable to store the recorded data in memory, so you have to store it on the SD Card. So, first check for the SD Card availability: then start recording with the following code.

    String status = Environment.getExternalStorageState();
    if(status.equals("mounted")){
       String path = your path;
    }
    
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(path);
    recorder.prepare();
    recorder.start();
    

    3) To stop, override stop method of activity

     recorder.stop();
     recorder.release();