1
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AudioRecordingActivity extends Activity {
    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 = "AudioRecorder";

private MediaRecorder recorder = null;
private int currentFormat = 0;
private int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4 };
private String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP4};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    setButtonHandlers();
    enableButtons(false);
    //setFormatButtonCaption();
}

private void setButtonHandlers() {
    ((Button) findViewById(R.id.btnStart)).setOnClickListener(btnClick);
    ((Button) findViewById(R.id.btnStop)).setOnClickListener(btnClick);
    //((Button) findViewById(R.id.btnFormat)).setOnClickListener(btnClick);
}

private void enableButton(int id, boolean isEnable) {
    ((Button) findViewById(id)).setEnabled(isEnable);
}

private void enableButtons(boolean isRecording) {
    enableButton(R.id.btnStart, !isRecording);
    //enableButton(R.id.btnFormat, !isRecording);
    enableButton(R.id.btnStop, isRecording);
}

/*private void setFormatButtonCaption() {
    ((Button) findViewById(R.id.btnFormat))
            .setText(getString(R.string.audio_format) + " ("
                    + file_exts[currentFormat] + ")");
}*/

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

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

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

private void startRecording() {
    recorder = new MediaRecorder();

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(output_formats[currentFormat]);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(getFilename());

    recorder.setOnErrorListener(errorListener);
    recorder.setOnInfoListener(infoListener);

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

private void stopRecording() {
    if (null != recorder) {
        recorder.stop();
        recorder.reset();
        recorder.release();

        recorder = null;
    }
}

private void displayFormatDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    String formats[] = { "MPEG 4", "3GPP" };

    builder.setTitle(getString(R.string.choose_format_title))
            .setSingleChoiceItems(formats, currentFormat,
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            currentFormat = which;
                            //setFormatButtonCaption();

                            dialog.dismiss();
                        }
                    }).show();
}

private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
    @Override
    public void onError(MediaRecorder mr, int what, int extra) {
        Toast.makeText(AudioRecordingActivity.this,
                "Error: " + what + ", " + extra, Toast.LENGTH_SHORT).show();
    }
};

private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
    @Override
    public void onInfo(MediaRecorder mr, int what, int extra) {
        Toast.makeText(AudioRecordingActivity.this,
                "Warning: " + what + ", " + extra, Toast.LENGTH_SHORT)
                .show();
    }
};

private View.OnClickListener btnClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnStart: {
            Toast.makeText(AudioRecordingActivity.this, "Start Recording",
                    Toast.LENGTH_SHORT).show();

            enableButtons(true);
            startRecording();

            break;
        }
        case R.id.btnStop: {
            Toast.makeText(AudioRecordingActivity.this, "Stop Recording",
                    Toast.LENGTH_SHORT).show();
            enableButtons(false);
            stopRecording();

            break;
        }
        /*case R.id.btnFormat: {
            displayFormatDialog();

            break;
        }*/
        }
    }
};
}

The above code records the audio and stores it in the sd card on click of a button to start and stop recording.

I want to make a recorder app which automatically starts recording audio for around 30 secs on starting the app.Is it possible to do so? If so then how?

mani bharataraju
  • 162
  • 1
  • 21

1 Answers1

0

You can issue a delayed task in your application's onCreate or onResume method that will start the recording after 30 seconds.

If you want to limit the recorded time, issue a delayed task after the recording has started.

Question previously asked about delayed tasks here: How to call a method after a delay in Android

You can also look here for more info: Handler vs AsyncTask

Community
  • 1
  • 1
lucian.pantelimon
  • 3,673
  • 4
  • 29
  • 46
  • i found this while searching on the internet setMaxDuration(int millisecs). what is the difference between what you suggested and this? – mani bharataraju Mar 19 '13 at 08:59
  • Not much difference I think. There might be cases when running a delayed task would be better. A first example that comes to mind would be the need for additional checks (besides that the recording has been stopped). If you do not have such requirements, you should use `setMaxDuration` instead of my solution in this case, as it makes the code more readable and easier to maintain. – lucian.pantelimon Mar 19 '13 at 09:16
  • 1
    Additional explanation for the delayed task case in the previous comment: you might want to check if the user has finished speaking when the 30 second mark is reached and if not, you might want to continue recording for, say, an additional 5 seconds and re-check after that. You won't be able to do this that easily when using `setMaxDuration`. – lucian.pantelimon Mar 19 '13 at 09:19
  • i don't want to do that,I actually want the app to start fresh and record again.Is there any way to do that? – mani bharataraju Mar 19 '13 at 09:21
  • hey can you check my previous question and help me out if possible – mani bharataraju Mar 19 '13 at 09:24
  • If the question you are referring to is this question, the answer is right here. Create a `Handler` in your `onCreate` method, then add a `Runnable` to it using its `postDelayed` method and a timeout of 30000ms. In your `Runnable`'s `run` method, call the `start` method on your `MediaRecorder` instance. This can be easily deduced from the answers to the questions given as reference in my answer. – lucian.pantelimon Mar 19 '13 at 09:40