start Media Record - Android Media

Android examples for Media:Audio Record

Description

start Media Record

Demo Code


import java.io.File;
import java.io.IOException;
import java.util.Date;
import android.media.MediaRecorder;
import android.os.Environment;

public class Main{
    //from   w w  w.j  a  v  a2s .c  o m
    public static String filePath = Environment
            .getExternalStorageDirectory() + "/exiu/cache/audio/";
    private static MediaRecorder recorder = new MediaRecorder();
    
    public static String startRecord() {
        String fileName = new Date().getTime() + ".amr";
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        recorder.reset();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        // 
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        // ?
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        // 

        recorder.setOutputFile(filePath + fileName);// SD?
        try {
            recorder.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 
        recorder.start(); // 

        return filePath + fileName;
    }
}

Related Tutorials