start Recording audio and choose format - Android android.media

Android examples for android.media:Audio

Description

start Recording audio and choose format

Demo Code

import java.io.IOException;

import android.media.MediaRecorder;
import android.util.Log;

public class Main{


    private static MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
        @Override//from   ww  w.  j a  v a  2s .  c  om
        public void onInfo(MediaRecorder mr, int what, int extra) {
            Log.e("Warning: ", "what-> " + what + ", " + extra);
        }
    };
    private static int currentFormat = 0;
    private static MediaRecorder recorder = null;
    private static String fileName;
    private static MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
        @Override
        public void onError(MediaRecorder mr, int what, int extra) {
            Log.e("Error: ", "what-> " + what + ", " + extra);
        }
    };
    private static int output_formats[] = {
            MediaRecorder.OutputFormat.MPEG_4,
            MediaRecorder.OutputFormat.THREE_GPP };
    public static void startRecording() {
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(output_formats[currentFormat]);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        fileName = "fileName.mp4";
        recorder.setOutputFile(fileName);
        recorder.setOnErrorListener(errorListener);
        recorder.setOnInfoListener(infoListener);

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


}

Related Tutorials