I'm trying to record sound into an array (short or bytes) using AudioRecord (MediaRecorder worked like a charm but it only records to files, and not to arrays). However, with the code below I am unable to record. When I try to print the contents of my array all I get is zeroes. I use my real device to record, and not the emulator.
I would appreciate any suggestions.
public class SoundCapture extends Activity {
private int _buffer_size;
public final int SAMPLE_RATE = 8000;
private AudioRecord _recorder;
private short[] _wave;
public boolean captureSound() {
_buffer_size = AudioRecord.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
_wave = new short[_buffer_size];
_recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, _buffer_size);
_recorder.startRecording();
//Recording for 5 seconds
synchronized(this) {
try{
this.wait(5000);
}
catch(InterruptedException e) {
e.printStackTrace();
return false;
}
}
_recorder.read(_wave, 0, SAMPLE_RATE);
_recorder.stop();
_recorder.release();
_recorder = null;
printWave();
return true;
}
private void printWave() {
System.out.println("Let the wave begin!!! ");
for(int i = 0; i < _wave.length; ++i) {
System.out.print(_wave[i] + ", ");
}
System.out.println("The End!!! ");
}
}