package com.example.kingway.ptt;

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

import com.stand.standapp.MyApplication;

import java.util.Arrays;

@SuppressWarnings("ResultOfMethodCallIgnored")
public class AudioRecordManager {
    private final String TAG = "AudioRecordManager";
    private static final int FREQUENCY = 8000;                  //16K采集率
    private AudioRecord mRecorder;
    private Thread recordThread;
    private boolean isStart = false;
    private static AudioRecordManager mInstance;
    private int bufferSize = 320;


    private AudioRecordManager() {
        initAudio();
    }

    public void initAudio() {
        //一、实例化一个AudioRecord类
        int audioSource = MediaRecorder.AudioSource.MIC;
        mRecorder = new AudioRecord(audioSource,
                FREQUENCY,
                AudioFormat.CHANNEL_IN_MONO,
                AudioFormat.ENCODING_PCM_16BIT,
                bufferSize * 2);
    }

    public void setRecording(){
        if (mRecorder == null) initAudio();
        if(mRecorder.getRecordingState() != 3) mRecorder.startRecording();
    }

    public void stopAudio(){
        if (mRecorder != null){
            mRecorder.stop();
        }
    }

    public int getState(){
        if (mRecorder == null) initAudio();
        return mRecorder.getRecordingState();
    }

    public void destroyAudio(){
        if (mRecorder != null){
            mRecorder.stop();
            mRecorder.release();
            mRecorder = null;
        }
    }


    /**
     * 获取单例引用
     */
    public static AudioRecordManager getInstance() {
        if (mInstance == null) {
            synchronized (AudioRecordManager.class) {
                if (mInstance == null) {
                    mInstance = new AudioRecordManager();
                }
            }
        }
        return mInstance;
    }

    /**
     * 销毁线程方法
     */
    private void destroyThread() {
        try {
            if (null != recordThread && Thread.State.RUNNABLE == recordThread.getState()) {
                try {
                    recordThread.interrupt();
                } catch (Exception e) {
                    recordThread = null;
                }
            }
            recordThread = null;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            recordThread = null;
        }
    }

    /**
     * 启动录音线程
     */
    private void startThread() {
        destroyThread();
        isStart = true;
        if (mRecorder == null) {
            initAudio();
        }
        if (recordThread == null) {
            recordThread = new Thread(recordRunnable);
            recordThread.start();
        }
    }

    /**
     * 录音线程
     */
    private Runnable recordRunnable = new Runnable() {
        @Override
        public void run() {
            try {
                android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
                int bytesRecord;
                byte[] tempBuffer = new byte[bufferSize];
                if (mRecorder.getState() != AudioRecord.STATE_INITIALIZED) {
                    stopRecord();
                    return;
                }
                mRecorder.startRecording();
                int count = 0;
                while (isStart) {
                    if (null != mRecorder) {
                        bytesRecord = mRecorder.read(tempBuffer, 0, bufferSize);
                        if (bytesRecord == AudioRecord.ERROR_INVALID_OPERATION
                                || bytesRecord == AudioRecord.ERROR_BAD_VALUE) {
                            continue;
                        }
                        if (bytesRecord != 0 && bytesRecord != -1) {
                            Log.i(TAG, "tempBuffer->" + Arrays.toString(tempBuffer));
                            MyApplication.getAppInstance().getpNative().pttNativeSendRecordCmd(tempBuffer);
                        } else {
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    };

    /**
     * 启动录音
     */
    public void startRecord() {
        try {
            stopRecord();

            startThread();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 停止录音
     */
    public void stopRecord() {
        try {
            destroyThread();
            if (mRecorder != null) {
                try {
                    if (mRecorder.getState() == AudioRecord.STATE_INITIALIZED) {
                        mRecorder.stop();
                    }
                    mRecorder.release();
                    mRecorder = null;
                } catch (Exception e) { }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }

}