Example usage for android.media AudioRecord ERROR_INVALID_OPERATION

List of usage examples for android.media AudioRecord ERROR_INVALID_OPERATION

Introduction

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

Prototype

int ERROR_INVALID_OPERATION

To view the source code for android.media AudioRecord ERROR_INVALID_OPERATION.

Click Source Link

Document

Denotes a failure due to the improper use of a method.

Usage

From source file:info.guardianproject.iocipher.camera.VideoCameraActivity.java

private void startAudioRecording() {

    Thread thread = new Thread() {

        public void run() {

            if (useAAC) {
                try {
                    aac.startRecording(outputStreamAudio);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }//w  w w .  jav a  2 s .  com
            } else {
                audioRecord.startRecording();

                while (mIsRecording) {
                    int audioDataBytes = audioRecord.read(audioData, 0, audioData.length);
                    if (AudioRecord.ERROR_INVALID_OPERATION != audioDataBytes && outputStreamAudio != null) {
                        try {
                            outputStreamAudio.write(audioData, 0, audioDataBytes);

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

                audioRecord.stop();
                try {
                    outputStreamAudio.flush();
                    outputStreamAudio.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    };

    thread.start();

}

From source file:com.example.sensingapp.SensingApp.java

private void processAudioData() {
    String sFullAudioPathFileTmp = m_sRecordFullPathFile + ".wav.tmp";
    FileOutputStream os = null;/*from ww  w.  ja v a2  s.c  o  m*/

    m_btAudioBuffer = new byte[m_nBufferSize];
    m_nAudioReadCount = AudioRecord.ERROR_INVALID_OPERATION;

    if (m_blnRecordSoundFile == true) {
        try {
            os = new FileOutputStream(sFullAudioPathFileTmp);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    while (m_audioRecorder != null) {

        m_nAudioReadCount = m_audioRecorder.read(m_btAudioBuffer, 0, m_nBufferSize);

        if (m_nAudioReadCount != AudioRecord.ERROR_INVALID_OPERATION) {

            if (m_blnRecordSoundFile == true && os != null) {
                try {
                    os.write(m_btAudioBuffer);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    } //While

    if (m_blnRecordSoundFile == true && os != null) {
        try {
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

From source file:com.example.sensingapp.SensingApp.java

private void calculateAudioSoundLevel() {
    long nTotalVal;
    int nLen;/*from   ww w.ja v a 2 s . c o  m*/
    short shtBuffer[];

    while (m_soundLevelThread != null && m_blnRecordSoundLevel == true) {

        if (m_nAudioReadCount != AudioRecord.ERROR_INVALID_OPERATION) {
            nTotalVal = 0;

            shtBuffer = DataUtil.Bytes2Shorts(m_btAudioBuffer, m_nAudioReadCount);

            nLen = shtBuffer.length;

            for (int i = 0; i < nLen; i++) {
                nTotalVal = nTotalVal + shtBuffer[i] * shtBuffer[i];
            }

            double fMean = nTotalVal * 1.0 / nLen;

            if (fMean == 0) {
                m_fSoundLevelDb = 1000;
            } else {
                m_fSoundLevelDb = 10 * Math.log10(fMean);
            }

            if (Double.isInfinite(m_fSoundLevelDb) || Double.isNaN(m_fSoundLevelDb)) {

            } else {
                recordSoundLevel(m_fSoundLevelDb);
            }

        } // if

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {

        }
    } // while
}

From source file:com.example.sensingapp.SensingApp.java

private void writeAudioDataToFile() {
    byte data[] = new byte[m_nBufferSize];
    String sFullAudioPathFileTmp = m_sRecordFullPathFile + ".wav.tmp";
    FileOutputStream os = null;/*from  w  ww .  ja  v  a2  s  . co  m*/

    try {
        os = new FileOutputStream(sFullAudioPathFileTmp);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    int read = 0;

    if (os != null) {
        while (m_audioRecorder != null) {
            read = m_audioRecorder.read(data, 0, m_nBufferSize);

            if (AudioRecord.ERROR_INVALID_OPERATION != read) {
                try {
                    os.write(data);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        try {
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}