AudioRecord如何保存文件(PCM.WAV)?

时间:2011-02-10 09:20:46

标签: android android-audiorecord

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();
但是,打开保存文件(reverseme.pcm),无法播放。 帮助我,谢谢。

1 个答案:

答案 0 :(得分:0)

您犯了一个大错:当您设置AudioRecord.getMinBufferSize的参数时,您选择channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO,但您可以查找应设置的字典channelConfig描述音频通道的配置。请参阅CHANNEL_IN_MONOCHANNEL_IN_STEREO