package com.fitbot.audio;

import android.media.MediaRecorder;

public class AudioRecorder {

    private MediaRecorder recorder;


    public AudioRecorder() {
    }

    /**
     * Record audio from the device's microphone
     * @return An instance of the AudioRecorder class
     */
    public AudioRecorder record() {

        recorder = new MediaRecorder();

        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        // Attempt to prepare and start the audio recorder
        try {
            recorder.prepare();
            recorder.start();
        } catch (Exception e) {
            System.err.println("Error preparing audio recorder");
            e.printStackTrace();
        }

        return this;
    }

    /**
     * Stop the audio recording
     */
    public void stop() {
        recorder.stop();
        recorder.release();
        recorder = null;
    }


}
