2011-02-10 46 views
0
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),無法播放。 幫幫我,謝謝。AudioRecord如何保存文件(PCM.WAV)?

回答

0

您正在犯一個很大的錯誤:當您設置AudioRecord.getMinBufferSize的參數時,您選擇channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO,但可以查找應該設置的字典channelConfig描述音頻通道的配置。見CHANNEL_IN_MONOCHANNEL_IN_STEREO

+0

嗨,如果你找到解決方案,請與我分享。 – 2012-03-29 10:29:20