Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.media.MediaRecorder;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

public class Main {
    private static final String TAG = "AudioRecordTest";
    private static MediaRecorder mRecorder = null;
    private static String path = "/sdcard/heardsensor/";
    private static boolean haveStarted = false;
    public static String currentFilePath = "";

    public static void startRecord() {
        if (!haveStarted) {

            haveStarted = true;

            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

            File pp = new File(path);
            if (!pp.isDirectory() && !pp.exists()) {
                pp.mkdir();
            }

            currentFilePath = path + new SimpleDateFormat("yyyyMMddHHmmss").format(System.currentTimeMillis())
                    + ".mp3";
            File saveFilePath = new File(currentFilePath);

            mRecorder.setOutputFile(saveFilePath.getAbsolutePath());

            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

            try {
                mRecorder.prepare();
            } catch (IOException e) {
                Log.e(TAG, "prepare() failed");
            }
            mRecorder.start();
        }
    }
}