So, The following code works perfectly:
Using java Syntax Highlighting
- int frequency = 11025;
- int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
- int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
- try {
- OutputStream os = new FileOutputStream(file);
- BufferedOutputStream bos = new BufferedOutputStream(os);
- DataOutputStream dos = new DataOutputStream(bos);
- int bufferSize = AudioRecord.getMinBufferSize(frequency,
- channelConfiguration, audioEncoding);
- AudioRecord audioRecord = new AudioRecord(
- MediaRecorder.AudioSource.MIC, frequency,
- channelConfiguration, audioEncoding, bufferSize);
- short[] buffer = new short[bufferSize];
- boolean shouldWriteToFile = false;
- audioRecord.startRecording();
- while (isRecording()) {
- int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
- for (int i = 0; i < bufferReadResult; i++){
- dos.writeShort(buffer[i]);
- }
- }
- audioRecord.stop();
- dos.close();
- } catch (Throwable t) {
- Log.e("AudioRecord", "Recording Failed");
- }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
but the following (where i'm not necesarry writing the entire buffer)will generate files that usually end up with "invalid audio buffer size" when trying to play them using AudioTrack;
Using java Syntax Highlighting
- int frequency = 11025;
- int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
- int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
- try {
- OutputStream os = new FileOutputStream(file);
- BufferedOutputStream bos = new BufferedOutputStream(os);
- DataOutputStream dos = new DataOutputStream(bos);
- int bufferSize = AudioRecord.getMinBufferSize(frequency,
- channelConfiguration, audioEncoding);
- AudioRecord audioRecord = new AudioRecord(
- MediaRecorder.AudioSource.MIC, frequency,
- channelConfiguration, audioEncoding, bufferSize);
- short[] buffer = new short[bufferSize];
- boolean shouldWriteToFile = false;
- audioRecord.startRecording();
- while (isRecording()) {
- int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
- for (int i = 0; i < bufferReadResult; i++){
- if (Math.abs(buffer[i]) > (MIN_AMPLITUDE)){
- shouldWriteToFile = true;
- }
- if (shouldWriteToFile)
- dos.writeShort(buffer[i]);
- }
- }
- audioRecord.stop();
- dos.close();
- } catch (Throwable t) {
- Log.e("AudioRecord", "Recording Failed");
- }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

