【问题标题】:Android: audioRecorder.getState() sends 0 in Samsung Galaxy TabAndroid:audioRecorder.getState() 在三星 Galaxy Tab 中发送 0
【发布时间】:2013-08-16 05:54:33
【问题描述】:

我正在使用下面的代码来获取设备的有效采样率,但对于我的三星 Galaxy Tab 1 OS 版本 2.3.6,它总是为 audioRecorder.getState() 发送 0,但对于三星 Galaxy S2 则很好。

我总是得到 desiredRate = -1; 的 Galaxy Tab 方法

public static void getValidSampleRates() {
        int desiredRate = 0;
        for (int rate : new int[] {44100, 8000, 11025, 16000, 22050}) {  // add the rates you wish to check against
            int bufferSize = AudioRecord.getMinBufferSize(rate, AudioFormat.CHANNEL_CONFIGURATION_DEFAULT, AudioFormat.ENCODING_PCM_16BIT);
            if (bufferSize > 0) {
                // buffer size is valid, Sample rate supported
                AudioRecord audioRecorder = new AudioRecord(AudioSource.MIC, rate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, 21168);

                if (audioRecorder.getState() != AudioRecord.STATE_INITIALIZED)
                    desiredRate = -1;
                else
                    desiredRate = rate;
            }
        }
    }

我在 Android manifest 文件中添加了以下权限

 <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="14" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />

    <application

请帮忙

【问题讨论】:

  • 你在 logcat 中得到任何错误打印吗?我本来希望AudioRecord 构造函数打印错误。如果您将bufferSize 传递给AudioRecord 构造函数而不是使用硬编码的21168 值会怎样?
  • 尝试将您的代码更改为int bufferSize = AudioRecord.getMinBufferSize(rate, AudioFormat.CHANNEL_IN_DEFAULT, AudioFormat.ENCODING_PCM_16BIT);,并将instatiaton 行更改为AudioRecord audioRecorder = new AudioRecord(AudioSource.MIC, rate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize+2000);

标签: java android audio audiorecord


【解决方案1】:

我在这里看到两个问题:

  1. 您正在为bufferSizeInBytes 使用硬编码值。根据documentation,如果使用小于getMinBufferSize 的值将导致初始化失败。为什么不在构造函数中使用bufferSize 的值?另外,在调用getMinBufferSize 时使用相同的channelConfig 值。

  2. question 中的答案类似,可能有人没有发布 AudioRecord 资源。您可以对 if 语句进行一些更改,如下面的代码块所示。如果这是真的,那么您的代码将在尝试初始化 44100 的比特率时失败,正确释放资源,然后成功地初始化 8000 的比特率。如果您仍然想要 44100 的比特率,请使用new int[] {44100, 44100, 8000, 11025, 16000, 22050}

下面的代码块包含对您的代码所做的两项更改。请尝试一下,然后告诉我你得到了什么。

public static void getValidSampleRates() {
    int desiredRate = 0;
    for (int rate : new int[] {44100, 8000, 11025, 16000, 22050}) {
        int bufferSize = AudioRecord.getMinBufferSize(rate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
        if (bufferSize > 0) {
            // buffer size is valid, Sample rate supported
            AudioRecord audioRecorder = new AudioRecord(AudioSource.MIC, rate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);

            if (audioRecorder.getState() != AudioRecord.STATE_INITIALIZED) {
                desiredRate = -1;
                audioRecorder.release();
            } else {
                desiredRate = rate;
                break;
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多