2013-10-06 30 views
2

我读过很多关于AudioRecorder初始化问题的帖子,所以我用一个函数来测试每一种可能性。 不幸的是,没有工作。我应该检查什么?AudioRecorder无法初始化后尝试每个变化

@Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Log.w("DEBUG", "Audio record"); 

    // Capture mono data at 16kHz 
    int frequency = 44100; 
    int channelConfiguration = AudioFormat.CHANNEL_IN_MONO; 
    int audioEncoding = AudioFormat.ENCODING_PCM_16BIT; 
    AudioRecord audioRecord=null; 

    // The minimal buffer size CANNOT be merely 20ms of data, it must be 
    // at least 1024 bytes in this case, this is most likely due to a MMIO 
    // hardware limit. 
    final int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding); 
    Log.w("DEBUG", "Buffer size: "+bufferSize); 
    try 
    { 

//  audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, 
//    frequency, channelConfiguration, 
//    audioEncoding, bufferSize); 
     audioRecord=findAudioRecord(); 

     short[] buffer = new short[bufferSize]; 
     if(audioRecord==null || audioRecord.getState()!= AudioRecord.STATE_INITIALIZED) 
     { 
     Log.w("DEBUG", "Can't start"); 
     //Log.w("DEBUG", "status: " + audioRecord.getState()); 
     return; 
     } 

     audioRecord.startRecording(); 


     // Blocking loop uses about 40% of the CPU to do this. 
     int sampleNumber = 0; 

     // We'll capture 3000 samples of 20ms each, 
     // giving us 60 seconds of audio data. 
     while (sampleNumber < 3000) 
     { 
     audioRecord.read(buffer, 0, 320); 

//  for (int i = 0; i < buffer.length; i++) 
//  { 
//   fileBuffer[i * 2] = (byte) (buffer[i] & (short) 0xFF); 
//   fileBuffer[i * 2 + 1] = (byte) (buffer[i] >> 8); 
//  } 


     sampleNumber++; 
     } 
    } catch (IllegalArgumentException e) 
    { 
     e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
    } catch (IllegalStateException e) 
    { 
     e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
    } 
    finally 
    { 
     if(audioRecord!=null && audioRecord.getState()==AudioRecord.STATE_INITIALIZED) 
     { 
     audioRecord.stop(); 
     audioRecord.release(); 
     } 
    } 


    } 

    private static int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 }; 
    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.w("DEBUG", "Attempting rate " + rate + "Hz, bits: " + audioFormat + ", channel: " 
        + channelConfig); 
      int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat); 

      if (bufferSize != AudioRecord.ERROR_BAD_VALUE) { 
       // check if we can instantiate and have a success 
       AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize); 

       if (recorder.getState() == AudioRecord.STATE_INITIALIZED) 
       return recorder; 
      } 
      } catch (Exception e) { 
      Log.w("DEBUG", rate + "Exception, keep trying.",e); 
      } 
     } 
     } 
    } 
    return null; 
    } 

这里是输出:

10-06 22:18:33.828:WARN/DEBUG(26097):试图率8000Hz的,位: 3,信道:16 10-06 22 :18:33.828:WARN/DEBUG(26097):尝试速率 8000Hz,bits:3,channel:12 10-06 22:18:33.828:WARN/DEBUG(26097): 尝试速率8000Hz,bits:2,channel :16 10-06 22:18:33.867: WARN/DEBUG(26097):尝试速率8000Hz,bits:2,channel:12 10-06 22:18:33.875:WARN/DEBUG(26097):尝试速率11025Hz ,比特:3, 频道:16 10-06 22:18:33.875:WARN/DEBUG(26097):尝试速率 11025Hz,bits:3,channel:12 10-06 22:18:33.875:WARN/DEBUG(26097): 正在尝试速率11025Hz,比特:2,信道:16 10-06 22:18:33.906: WARN/DEBUG(26097):尝试速率11025Hz,比特:2,信道:12 10-06 22:18:33.929:WARN/DEBUG(26097):尝试速率22050Hz,bits:3, 通道:16 10-06 22:18:33.929:WARN/DEBUG(26097):尝试速率 22050Hz,bits:3,channel:12 10-06 22: 18:33.929:WARN/DEBUG(26097): 尝试速率22050Hz,bits:2,通道:16 10-06 22:18:33.929: WARN/DEBUG(26097):尝试速率22050Hz,bits:2,通道: 12 10-06 22:18:33.929:WARN/DEBUG(26097):尝试速率44100Hz,bits:3,频道:16 10-06 22:18:33.929:WARN/DEBUG(26097)速率44100Hz,bits:2,channel:16 10-06 22:18:33.937: WARN/DEBUG(26097):尝试速率44100Hz,bits:2,channel:12 10-06 22:18:33.937:WARN/DEBUG(26097):无法启动

我已在在清单如下:

使用-SDK安卓的minSdkVersion = “10” 机器人:targetSdkVersion = “16” 使用许可权机器人:名字=“android.permi ssion.RECORD_AUDIO“

我的手机是Galaxy Ace。 我应该检查什么?

回答

2

我重新启动手机,它现在可以工作。我想,在每种情况下拨打release()(请参阅finally区块)是必不可少的

+0

你能解释更多吗? – Josh