package com.app.sounds.prank.party.common;

import android.content.Context;
import android.media.MediaRecorder;

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

public class Manager {
    private MediaRecorder mediaRecorder;
    private String tempFileName;
    private String fileName;
    private boolean isRecording = false;

    public Manager(Context context) {

        File dir = context.getCacheDir();
        File file = new File(dir, "temp_audio.mp3");
        tempFileName = file.getAbsolutePath();
    }


    public void startRecording() {
        if (isRecording) return;
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setOutputFile(tempFileName);

        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
            isRecording = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void stopRecording() {
        if (!isRecording) return;

        try {
            mediaRecorder.stop();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } finally {
            mediaRecorder.release();
            mediaRecorder = null;
            isRecording = false;
        }
    }

    public String getTempFileName() {
        return tempFileName;
    }
}
