android voice recorder application

star angel picture star angel · May 12, 2011 · Viewed 9k times · Source

I want to create a voice recorder application in android 3.0. The scenario is when i click on record button it has to start record and when I click on stop button it has to stop recording.

After recording voice I want to play the recorded sound when I click on play button. and it has to stop playing when I click on stop playing button. I had tried a code to record voice. but it's storing the file in 3gpp format. so it s not playing in ma device. so i tried to change the outputformat to 'AMR_NB'.but it is crashing when i click on stop button. can anyone provide a code otherwise plz help me to make it work by editing my code. This is my helper class...

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.AMR_NB);
    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();
  }
}

and this is my activity class

public class audiorecording extends Activity {
private Button start;
private Button stop;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    start=(Button)findViewById(R.id.start);
    stop=(Button)findViewById(R.id.stop);
    final AudioRecorder recorder = new AudioRecorder("/audiometer/temp");



    start.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
             try {
                    recorder.start();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


        }
    })  ;       

    //….wait a while

        stop.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                try {
                    recorder.stop();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        })  ;

    }
}

Answer

Ahmad Kayyali picture Ahmad Kayyali · May 12, 2011

You can use setOutputFormat (int output_format)

Sets the format of the output file produced during recording. Call this after setAudioSource()/setVideoSource() but before prepare(). It is recommended to always use 3GP format when using the H.263 video encoder and AMR audio encoder. Using an MPEG-4 container format may confuse some desktop players.


Audalyzer An audio analyzer for Android

This is a simple audio analyser for Android. It displays sound readings from the microphone as a waveform display, as a frequency spectrum, and as a dB meter. dB levels are relative to the maximum input level of your device.


Android MediaRecorder

A common case of using MediaRecorder to record audio works as follows:

MediaRecorder recorder = new MediaRecorder();
 recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
 recorder.setOutputFile(PATH_NAME);
 recorder.prepare();
 recorder.start();   // Recording is now started
 ...
 recorder.stop();
 recorder.reset();   // You can reuse the object by going back to setAudioSource() step
 recorder.release(); // Now the object cannot be reused