package io.github.mightybeards.flyting.domain;

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

import java.io.File;
import java.io.IOException;

public class AudioRecorder {

    private MediaRecorder recorder;
    private String path;
    private int currentRecord = 1;

    public AudioRecorder(String path) {
        this.path = path += "/" + System.currentTimeMillis();
        new File(path).mkdir();
    }

    /*
     *  Returns the path to the newly written file.
     */
    public String startRecord() {
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        String outputFile = path + "/" + currentRecord + ".mp4";
        recorder.setOutputFile(outputFile);
        try {
            recorder.prepare();
        } catch (IOException e) {
            Log.e("AudioRecorder", "prepare() failed");
        }
        recorder.start();

        return outputFile;
    }

    public void stopRecord() {
        recorder.stop();
        recorder.reset();
        recorder.release();
        currentRecord++;
    }

}
