package net.micode.notes.ui;

import static net.micode.notes.R.id.start_record;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import net.micode.notes.R;

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


public class RecordActivity extends Activity {

    private MediaRecorder mediaRecorder;

    private String TAG = "RecordActivity";

    private boolean isRecording;

    private Button start;
    private Button stop;
    private Button play;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.record_activity);
        start = (Button) findViewById (start_record);
        stop = (Button) findViewById (R.id.stop_record);
        play = (Button) findViewById (R.id.play_record);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                start();
                Log.d(TAG, "onClick: Start");
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stop();
                Log.d(TAG, "onClick: Stop");
            }
        });

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "onClick: play");
            }
        });
    }

    //在这里重载Back键执行的操作
    /*@Override
    public void onBackPressed(){

    }*/

    private boolean checkSDCard() {
        // TODO Auto-generated method stub
        //检测SD卡是否插入手机中
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return true;
        }
        return false;
    }

    /**
     * 开始录音
     */
    protected void start() {
        try {
            File path;
            if (checkSDCard()) {
                File mpath = Environment.getExternalStorageDirectory();
                path = new File(mpath.getPath() + File.separator + "audioRecords");
                Log.d(TAG, "start: "+path);
                if (!path.mkdirs()) {
                    Log.d("audioRecorder", "创建目录失败");
                    return;
                }

            } else {
                Toast.makeText(RecordActivity.this, "SDcard未连接",
                        Toast.LENGTH_LONG).show();
                return;
            }

            File file = null;
            try {
                file = File.createTempFile(String.valueOf("myrecord_"), ".amr", path);
            } catch (IOException e) {
                Log.d("audioRecorder", "文件创建失败");
            }

            if (file.exists()) {
                // 如果文件存在，删除它，演示代码保证设备上只有一个录音文件
                file.delete();
            }
            mediaRecorder = new MediaRecorder();
            // 设置音频录入源
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            // 设置录制音频的输出格式
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            // 设置音频的编码格式
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            // 设置录制音频文件输出文件路径
            mediaRecorder.setOutputFile(file.getAbsolutePath());

            mediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {

                @Override
                public void onError(MediaRecorder mr, int what, int extra) {
                    // 发生错误，停止录制
                    mediaRecorder.stop();
                    mediaRecorder.release();
                    mediaRecorder = null;
                    isRecording=false;
                    start.setEnabled(true);
                    stop.setEnabled(false);
                    Toast.makeText(RecordActivity.this, "录音发生错误", Toast.LENGTH_SHORT).show();
                }
            });

            // 准备、开始
            mediaRecorder.prepare();
            mediaRecorder.start();

            isRecording=true;
            start.setEnabled(false);
            stop.setEnabled(true);
            Toast.makeText(RecordActivity.this, "开始录音", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 录音结束
     */
    protected void stop() {
        if (isRecording) {
            // 如果正在录音，停止并释放资源
            mediaRecorder.stop();
            mediaRecorder.release();
            mediaRecorder = null;
            isRecording=false;
            start.setEnabled(true);
            stop.setEnabled(false);
            Toast.makeText(RecordActivity.this, "录音结束", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        if (isRecording) {
            // 如果正在录音，停止并释放资源
            mediaRecorder.stop();
            mediaRecorder.release();
            mediaRecorder = null;
        }
        super.onDestroy();
    }
}