How do AudioRecord save a file (PCM.WAV)?

5k views Asked by At
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.

1

There are 1 answers

1
user642960 On

You are making a big mistake : when you set AudioRecord.getMinBufferSize's parameters,you choose channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO,but you can look up dictionary that is should set channelConfig describes the configuration of the audio channels. See CHANNEL_IN_MONO and CHANNEL_IN_STEREO.