Skip to content

Instantly share code, notes, and snippets.

@filipproch
Created January 24, 2015 17:59
Show Gist options
  • Save filipproch/7d29ac95c324ec132396 to your computer and use it in GitHub Desktop.
Save filipproch/7d29ac95c324ec132396 to your computer and use it in GitHub Desktop.
[ANDROID] - Record audio to ByteBuffer
class AudioRecordThread implements Runnable {
@Override
public void run() {
int bufferLength = 0;
int bufferSize;
short[] audioData;
int bufferReadResult;
try {
bufferSize = AudioRecord.getMinBufferSize(sampleAudioBitRate,
AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
if (bufferSize <= 2048) {
bufferLength = 2048;
} else if (bufferSize <= 4096) {
bufferLength = 4096;
}
/* set audio recorder parameters, and start recording */
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleAudioBitRate,
AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferLength);
audioData = new short[bufferLength];
audioRecord.startRecording();
Log.d(LOG_TAG, "audioRecord.startRecording()");
isAudioRecording = true;
/* ffmpeg_audio encoding loop */
while (isAudioRecording) {
bufferReadResult = audioRecord.read(audioData, 0, audioData.length);
if (bufferReadResult == 1024 && isRecorderStart) {
Buffer realAudioData1024 = ShortBuffer.wrap(audioData,0,1024);
***********************************
recorder.record(realAudioData1024);
***********************************
} else if (bufferReadResult == 2048 && isRecorderStart) {
Buffer realAudioData2048_1=ShortBuffer.wrap(audioData, 0, 1024);
Buffer realAudioData2048_2=ShortBuffer.wrap(audioData, 1024, 1024);
for (int i = 0; i < 2; i++) {
if (i == 0) {
***********************************
recorder.record(realAudioData2048_1);
***********************************
} else if (i == 1) {
***********************************
recorder.record(realAudioData2048_2);
***********************************
}
}
}
}
/* encoding finish, release recorder */
if (audioRecord != null) {
try {
audioRecord.stop();
audioRecord.release();
} catch (Exception e) {
e.printStackTrace();
}
audioRecord = null;
}
if (recorder != null && isRecorderStart) {
try {
recorder.stop();
recorder.release();
} catch (Exception e) {
e.printStackTrace();
}
recorder = null;
}
} catch (Exception e) {
Log.e(LOG_TAG, "get audio data failed:"+e.getMessage()+e.getCause()+e.toString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment