2012-10-26 61 views
2

我正在尝试使用AudioRecord,但我无法初始化录音。我有两台设备,其中一台运行良好,但另一台设备仍然有例外。初始化录音

我的代码:

bufferSize = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT); 
AudioRecord recorder = new AudioRecord(AudioSource.DEFAULT, rate,channelConfig, audioFormat, bufferSize); 

为什么这是错的,什么是这样做的正确方法?

回答

2
private int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 }; 
    int bufferSize; 

    AudioRecord audioInput = findAudioRecord(); 

    public AudioRecord findAudioRecord() { 
     for (int rate : mSampleRates) { 
      for (short audioFormat : new short[] { 
        AudioFormat.ENCODING_PCM_8BIT, 
        AudioFormat.ENCODING_PCM_16BIT }) { 
       for (short channelConfig : new short[] { 
         AudioFormat.CHANNEL_IN_MONO, 
         AudioFormat.CHANNEL_IN_STEREO }) { 
        try { 
         Log.d("Mic2", "Attempting rate " + rate 
           + "Hz, bits: " + audioFormat 
           + ", channel: " + channelConfig); 
         bufferSize = AudioRecord.getMinBufferSize(rate, 
           channelConfig, audioFormat); 

         if (RECORDINGDURATION * sampleRate != AudioRecord.ERROR_BAD_VALUE) { 
          // check if we can instantiate and have a 
          // success 
          AudioRecord recorder = new AudioRecord(
            AudioSource.DEFAULT, rate, 
            channelConfig, audioFormat, bufferSize); 
          if (recorder.getState() == AudioRecord.STATE_INITIALIZED) 
           sampleRate = rate; 
          return recorder; 
         } 
        } catch (Exception e) { 
         Log.e(TAG, rate + "Exception, keep trying.", e); 
        } 
       } 
      } 
     } 
     return null; 
    } 

该设备最有可能不支持16位编码 - > AudioFormat.ENCODING_PCM_16BIT :-)

+0

是的就是这样!最后,经过几个小时的努力,我的录音工作都在两台设备上进行。谢谢。 – slezadav

2

仿真器不支持CHANNEL_CONFIGURATION_STEREO & SAMPLERATE =(11025,16000,22050,44100和),如果你想成功运行这个CHANNEL_CONFIGURATION_MONO & SampleRate = 8000(它每秒只支持8000个样本)。

下面是做什么的详细说明:http://developer.android.com/guide/topics/media/audio-capture.html

+1

我已经有采样率设置为8000,此外我不使用模拟器来测试。 – slezadav