Example usage for android.media AudioRecord read

List of usage examples for android.media AudioRecord read

Introduction

In this page you can find the example usage for android.media AudioRecord read.

Prototype

public int read(@NonNull ByteBuffer audioBuffer, int sizeInBytes, @ReadMode int readMode) 

Source Link

Document

Reads audio data from the audio hardware for recording into a direct buffer.

Usage

From source file:edu.nchu.cs.dmlab.firemap.mjpeg.RecMicToMp3.java

/**
 * Start recording/*from   w w w.  j a v  a  2s .co  m*/
 */
public void start() {
    // just skip if recording has happened
    if (mIsRecording) {
        return;
    }

    new Thread() {
        @Override
        public void run() {
            if (mHandler != null)
                mHandler.sendEmptyMessage(Message.MSG_DIALOG_OPEN);
            android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
            // caculate minimax buffersize
            final int minBufferSize = AudioRecord.getMinBufferSize(mSampleRate, AudioFormat.CHANNEL_IN_MONO,
                    AudioFormat.ENCODING_PCM_16BIT);
            if (minBufferSize < 0) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(Message.MSG_ERROR_GET_MIN_BUFFERSIZE);
                }
                return;
            }
            AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, mSampleRate,
                    AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize * 2);
            // PCM buffer size (5sec)
            short[] buffer = new short[mSampleRate * (16 / 8) * 1 * 5]; // SampleRate[Hz] * 16bit * Mono * 5sec
            byte[] mp3buffer = new byte[(int) (7200 + buffer.length * 2 * 1.25)];

            FileOutputStream output = null;
            try {
                output = new FileOutputStream(mFile);
            } catch (FileNotFoundException e) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(Message.MSG_ERROR_CREATE_FILE);
                }
                return;
            }

            // Lame init
            SimpleLame.init(mSampleRate, 1, mSampleRate, 32);

            try {
                try {
                    audioRecord.startRecording();
                    mIsRecording = true;
                } catch (IllegalStateException e) {
                    if (mHandler != null) {
                        mHandler.sendEmptyMessage(Message.MSG_ERROR_REC_START);
                    }
                    return;
                }

                try {
                    if (mHandler != null) {
                        mHandler.sendEmptyMessage(Message.MSG_REC_STARTED);
                    }

                    int readSize = 0;
                    while (mIsRecording) {
                        readSize = audioRecord.read(buffer, 0, minBufferSize);
                        if (readSize < 0) {
                            if (mHandler != null) {
                                mHandler.sendEmptyMessage(Message.MSG_ERROR_AUDIO_RECORD);
                            }
                            break;
                            // no data
                        } else if (readSize == 0) {
                            ;
                            // fetch data
                        } else {
                            int encResult = SimpleLame.encode(buffer, buffer, readSize, mp3buffer);
                            if (encResult < 0) {
                                if (mHandler != null) {
                                    mHandler.sendEmptyMessage(Message.MSG_ERROR_AUDIO_ENCODE);
                                }
                                break;
                            }
                            if (encResult != 0) {
                                try {
                                    output.write(mp3buffer, 0, encResult);
                                } catch (IOException e) {
                                    if (mHandler != null) {
                                        mHandler.sendEmptyMessage(Message.MSG_ERROR_WRITE_FILE);
                                    }
                                    break;
                                }
                            }
                        }
                    }
                    int flushResult = SimpleLame.flush(mp3buffer);
                    if (flushResult < 0) {
                        if (mHandler != null) {
                            mHandler.sendEmptyMessage(Message.MSG_ERROR_AUDIO_ENCODE);
                        }
                    }
                    if (flushResult != 0) {
                        try {
                            output.write(mp3buffer, 0, flushResult);
                        } catch (IOException e) {
                            if (mHandler != null) {
                                mHandler.sendEmptyMessage(Message.MSG_ERROR_WRITE_FILE);
                            }
                        }
                    }

                    try {
                        output.close();
                    } catch (IOException e) {
                        if (mHandler != null) {
                            mHandler.sendEmptyMessage(Message.MSG_ERROR_CLOSE_FILE);
                        }
                    }
                } finally {
                    audioRecord.stop();
                    audioRecord.release();
                }
            } finally {
                SimpleLame.close();
                mIsRecording = false;
            }

            if (mHandler != null) {
                mHandler.sendEmptyMessage(Message.MSG_REC_STOPPED);
            }

            // Upload Audio
            uploadVoice();
            if (mHandler != null)
                mHandler.sendEmptyMessage(Message.MSG_DIALOG_CLOSE);
        }
    }.start();
}