static final int frequency = 8000;
static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
recBufSize = AudioRecord.getMinBufferSize(frequency,
channelConfiguration, audioEncoding);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency,
channelConfiguration, audioEncoding, recBufSize);
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+"/reverseme.pcm");
OutputStream os = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);
short[] buffer = new short[recBufSize];
audioRecord.startRecording();
while (isRecording) {
int bufferReadResult = audioRecord.read(buffer, 0,
recBufSize);
for(int i = 0; i < bufferReadResult;i++) {
dos.writeShort(buffer[i]);
}
}
audioRecord.stop();
dos.flush();
dos.close();
but, open the save file(reverseme.pcm), can not play. Help me, thanks.
You are making a big mistake : when you set
AudioRecord.getMinBufferSize
's parameters,you choosechannelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO
,but you can look up dictionary that is should set channelConfig describes the configuration of the audio channels. SeeCHANNEL_IN_MONO
andCHANNEL_IN_STEREO
.