Android AudioRecord 找不到工作配置

Android AudioRecord cannot find working configuration

我正在尝试缓冲音频数据以分析分贝水平。

我正在使用 AudioRecord class,因为它允许立即处理来自缓冲区的数据,而无需将它们存储在文件中。

我正在使用

<uses-permission android:name="ANDROID.PERMISSION.RECORD_AUDIO" />

许可。 编辑:这是错误的!

我在 onStart() 中通过

从我的主要 activity 启动它作为服务
Intent intent = new Intent(this, Audio2.class);
startService(intent);

服务在 onStartCommand(...) 期间调用 findAudioRecord()

private int[] sampleRates = new int[] { 44_100, 22_050, 11_025, 8_000 };
private short[] audioFormats = new short[] { AudioFormat.ENCODING_PCM_16BIT, AudioFormat.ENCODING_PCM_8BIT };
private short[] channelConfigs = new short[] { AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO };

public AudioRecord findAudioRecord() {
    for (int rate : sampleRates){
        for (short audioFormat : audioFormats){
            for (short channelConfig : channelConfigs){
                try {
                    Log.d(TAG, "Attempting rate " + rate + "Hz, " + audioFormat + " bits, channel: " + channelConfig);
                    bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);

                    if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {
                        AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize*10);

                        if (recorder.getState() == AudioRecord.STATE_INITIALIZED) {
                            return recorder;
                        } else {
                            recorder.release();
                        }
                    }
                } catch (Exception e) {
                    Log.e(TAG, rate + "Exception, keep trying.",e);
                }
            }
        }
    }
    Log.wtf(TAG, "No working configuration found!");
    return null;
}

这总是 returns null。我收到错误:

E/AudioRecord﹕ AudioFlinger could not create record track, status: -1
E/AudioRecord-JNI﹕ Error creating AudioRecord instance: initialization check failed.
E/AudioRecord-Java﹕ android.media.AudioRecord Error code -20 when initializing native AudioRecord object.

我使用的是三星 Galaxy S4。

我试过没有成功的:

经过很多个小时,我终于弄明白了问题所在。如果您使用 Android Studio 的自动完成功能并输入

<uses-permission android:name="RECORD_AUDIO" />

接着回车,AndroidStudio会填写

<uses-permission android:name="ANDROID.PERMISSION.RECORD_AUDIO" />

然而,这不起作用,因为大小写显然很重要。没有警告,什么都没有。我改成

<uses-permission android:name="android.permission.RECORD_AUDIO" />

终于成功了...