package com.mass.audio;

import android.media.AudioFormat;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.mass.audio.library.Recorder;
import com.mass.audio.library.model.OnByteBufferDataChangeListener;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

public class MainActivity extends AppCompatActivity implements OnByteBufferDataChangeListener {

    public static final String TEST_FILE_PATH = Environment.getExternalStorageDirectory() + "/test.pcm";

    private ImageButton mImageButton;

    private Recorder mRecorder;
    private FileOutputStream mStream;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageButton = (ImageButton) findViewById(R.id.action_image);
        mImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mRecorder == null) {
                    return;
                }
                boolean recording = mRecorder.isRecording();
                if (recording) {
                    ((ImageButton) v).setImageResource(R.drawable.record);
                    mRecorder.stop();
                } else {
                    ((ImageButton) v).setImageResource(R.drawable.pause);
                    mRecorder.startRecording();
                }
            }
        });

        boolean result = createOutputFile();
        if (!result) {
            Toast.makeText(this, "创建文件失败~", Toast.LENGTH_SHORT).show();
        }

        mRecorder = new Recorder(44100,
                AudioFormat.CHANNEL_IN_MONO/*单双声道*/,
                AudioFormat.ENCODING_PCM_16BIT/*格式*/,
                MediaRecorder.AudioSource.MIC/*AudioSource*/,
                512/*period*/,
                this/*onDataChangeListener*/);

    }

    private boolean createOutputFile() {
        try {
            mStream = new FileOutputStream(new File(TEST_FILE_PATH));
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void onDataChange(int position, ByteBuffer byteBuffer) {
        try {
            if (mStream != null) {
                mStream.write(byteBuffer.array());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@@ -745,16 +745,65 @@
 t.pcm%22;%0A
+    private static final int NUM_SAMPLES = 512;%0A%0A
 %0A    pri
@@ -905,16 +905,44 @@
 tream;%0A%0A
+    private byte%5B%5D output;%0A%0A
     @Ove
@@ -2162,19 +2162,27 @@
         
-512
+NUM_SAMPLES
 /*period
@@ -2231,16 +2231,60 @@
 ener*/);
+%0A        output = new byte%5BNUM_SAMPLES * 2%5D;
 %0A%0A    %7D%0A
@@ -2619,20 +2619,16 @@
 Buffer b
-yteB
 uffer) %7B
@@ -2708,20 +2708,16 @@
 .write(b
-yteB
 uffer.ar
@@ -2820,11 +2820,313 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * %E9%80%82%E7%94%A8%E4%BA%8E%E5%B0%8F%E7%AB%AF%E5%9C%A8%E5%89%8D%E7%9A%84short%E8%BD%AC%E6%8D%A2%E6%88%90byte%0A     *%0A     * @param value 16%E4%BD%8Dshort%E5%80%BC%0A     * @return byte%5B%5D%E6%95%B0%E7%BB%84%0A     */%0A    private byte%5B%5D short2byte(short value) %7B%0A        byte%5B%5D data = new byte%5B2%5D;%0A        data%5B0%5D = (byte) (value & 0xFF);%0A        data%5B1%5D = (byte) ((value %3E%3E 8) & 0xFF);%0A        return data;%0A    %7D
 %0A%7D%0A

Add the function of shortConvertByteArray()