Search in sources :

Example 1 with AudioEncoder

use of com.amazonaws.mobileconnectors.lex.interactionkit.internal.audio.encoder.AudioEncoder in project aws-sdk-android by aws-amplify.

the class AudioRecorder method startRecording.

/**
 * Method to setup, start the recorder, and read data.
 * When recorder is stopped by the user, clean up resources.
 * @throws Exception when there are problems while recording audio.
 */
public void startRecording() throws Exception {
    startAudioRecorder();
    final short[] buffer = new short[mNumSamplesPerRead];
    int numSamplesRead;
    final AudioSourceListener listener = getAudioSourceListener();
    final AudioEncoder pcmEncoder = new L16PcmEncoder();
    try {
        Log.v(TAG, "Starting record loop");
        while (isInValidStateToContinueRecording()) {
            // Make sure recorder is not null before recording.
            if (mRecord == null) {
                Log.e(TAG, "Recorder is null.");
                throw new AudioSourceException("Recorder null");
            }
            // Buffer bytes to be sent to callback.
            synchronized (mRecord) {
                numSamplesRead = mRecord.read(buffer, 0, mNumSamplesPerRead);
            }
            final int invalidOperation = AudioRecord.ERROR_INVALID_OPERATION;
            if (invalidOperation != numSamplesRead) {
                setPostRecordingFields();
                if (numSamplesRead > 0) {
                    // Prepare samples for the callback.
                    final byte[] callbackBuffer = pcmEncoder.encode(buffer, numSamplesRead);
                    listener.onBufferReceived(callbackBuffer);
                    updateSumSamplesForRMSCalculations(numSamplesRead, buffer);
                }
                postAudioRecordingProcessing(numSamplesRead, buffer, listener);
            } else {
                Log.v(TAG, "AudioRecord - Invalid Operation");
                throw new AudioSourceException("AudioRecord - Invalid Operation");
            }
        }
        Log.v(TAG, "Finished record loop");
    } finally {
        cleanUpAfterRecording();
    }
}
Also used : AudioEncoder(com.amazonaws.mobileconnectors.lex.interactionkit.internal.audio.encoder.AudioEncoder) L16PcmEncoder(com.amazonaws.mobileconnectors.lex.interactionkit.internal.audio.encoder.L16PcmEncoder)

Aggregations

AudioEncoder (com.amazonaws.mobileconnectors.lex.interactionkit.internal.audio.encoder.AudioEncoder)1 L16PcmEncoder (com.amazonaws.mobileconnectors.lex.interactionkit.internal.audio.encoder.L16PcmEncoder)1