android - 在尝试每种变体后,AudioRecorder 无法初始化

标签 android audio-recording

我读过很多关于 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): Attempting rate 8000Hz, bits: 3, channel: 16 10-06 22:18:33.828: WARN/DEBUG(26097): Attempting rate 8000Hz, bits: 3, channel: 12 10-06 22:18:33.828: WARN/DEBUG(26097): Attempting rate 8000Hz, bits: 2, channel: 16 10-06 22:18:33.867: WARN/DEBUG(26097): Attempting rate 8000Hz, bits: 2, channel: 12 10-06 22:18:33.875: WARN/DEBUG(26097): Attempting rate 11025Hz, bits: 3, channel: 16 10-06 22:18:33.875: WARN/DEBUG(26097): Attempting rate 11025Hz, bits: 3, channel: 12 10-06 22:18:33.875: WARN/DEBUG(26097): Attempting rate 11025Hz, bits: 2, channel: 16 10-06 22:18:33.906: WARN/DEBUG(26097): Attempting rate 11025Hz, bits: 2, channel: 12 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 22050Hz, bits: 3, channel: 16 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 22050Hz, bits: 3, channel: 12 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 22050Hz, bits: 2, channel: 16 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 22050Hz, bits: 2, channel: 12 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 44100Hz, bits: 3, channel: 16 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 44100Hz, bits: 3, channel: 12 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 44100Hz, bits: 2, channel: 16 10-06 22:18:33.937: WARN/DEBUG(26097): Attempting rate 44100Hz, bits: 2, channel: 12 10-06 22:18:33.937: WARN/DEBUG(26097): Can't start

我的 list 中有以下内容:

uses-sdk android:minSdkVersion="10" android:targetSdkVersion="16" uses-permission android:name="android.permission.RECORD_AUDIO"

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

最佳答案

我重新启动了手机,现在可以使用了。我想,在每种情况下调用 release()(参见 finally block )是必要的

关于android - 在尝试每种变体后,AudioRecorder 无法初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19213531/

相关文章:

java - 如何在android中设置定时器?

android - 如何在客户端验证 APK 签名证书?

快速录音机

c# - 我们可以使用 C# 录制发送到扬声器的声音吗

android - android 6.0运行时需要哪些权限

android - 在 webview 中过早调用滑动刷新

Android:触发searchview时状态栏颜色变化

ios - 如何在 iOS 中录制声音?

android - 实时播放来自麦克风的声音

android - manifest.xml 中的 uses-feature required=false 未在 android 1.6 上编译