start Audio Recording - Android Media

Android examples for Media:Audio Record

Description

start Audio Recording

Demo Code


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

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

public class Main {
  private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp";
  private static final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4";
  private static final String AUDIO_RECORDER_FOLDER = "/ClickIn/Audio";
  private static MediaRecorder recorder = null;
  private static int currentFormat = 0;
  private static int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4, MediaRecorder.OutputFormat.THREE_GPP };
  private static String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP4, AUDIO_RECORDER_FILE_EXT_3GP };
  private static String fileName;
  private static MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
    @Override// ww w . j  av  a  2  s .  com
    public void onError(MediaRecorder mr, int what, int extra) {
      // Log.e("Error: ", "what-> " + what + ", " + extra);
    }
  };
  private static MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
    @Override
    public void onInfo(MediaRecorder mr, int what, int extra) {
      // Log.e("Warning: ", "what-> " + what + ", " + extra);
    }
  };

  public static void startRecording() {
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(output_formats[currentFormat]);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    fileName = getFilename();
    recorder.setOutputFile(fileName);
    recorder.setOnErrorListener(errorListener);
    recorder.setOnInfoListener(infoListener);

    try {
      recorder.prepare();
      recorder.start();
    } catch (IllegalStateException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  private static String getFilename() {
    String filepath = Environment.getExternalStorageDirectory().getAbsolutePath();
    File file = new File(filepath, AUDIO_RECORDER_FOLDER);

    if (!file.exists()) {
      file.mkdirs();
    }

    return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
  }
}

Related Tutorials