1

I am working on new app development in android. I need to launch an audio recording app from my current appication and i have launched it by calling intent using

MediaStore.Audio.Media.RECORD_SOUND_ACTION

But now i need to get control to the media which is being recorded. I need to record only for 30 seconds. the recording should be stopped when it reaches 30 seconds.

Any suggestions??

Navya Ramesan
  • 53
  • 1
  • 6

2 Answers2

5

i think CountDownTimer will be help you ::

CountDownTimer countDowntimer = new CountDownTimer(30000, 1000) {
                        public void onTick(long millisUntilFinished) {
                        }

                        public void onFinish() {
                            try {
                                Toast.makeText(context, "Stop recording Automatically ", Toast.LENGTH_LONG).show();
                                recorder.stop();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }


                }};countDowntimer.start();
Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
2

Use the MediaRecorder like so :

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

}

Use a Timer/TimerTask to call stop() after 30 seconds.

Source: tutorial

Community
  • 1
  • 1
Reno
  • 33,594
  • 11
  • 89
  • 102
  • Thanks for the suggestion. but for my current project, I need to use the built apps in android device. Now the problem I'm facing is that I cannot give a duration for recording. Any suggestions ?? – Navya Ramesan Nov 14 '11 at 11:46