Android - 初始化AudioRecord时出现所有参数组合的失败情况

3
我正在使用下面的代码查找可用的参数组合来创建一个AudioRecord对象。
public static AudioRecord findAudioRecord() {
    for (int rate : new int[]{8000, 11025, 16000, 22050, 44100}) {
        for (int audioFormat : new int[]{
                AudioFormat.ENCODING_DEFAULT,
                AudioFormat.ENCODING_PCM_16BIT,
                AudioFormat.ENCODING_PCM_8BIT,
                AudioFormat.ENCODING_PCM_FLOAT,
                AudioFormat.ENCODING_AC3,
                AudioFormat.ENCODING_E_AC3,
                AudioFormat.ENCODING_DTS,
                AudioFormat.ENCODING_DTS_HD,
        }) {
            for (int channelConfig : new int[]{
                    AudioFormat.CHANNEL_IN_DEFAULT,
                    AudioFormat.CHANNEL_IN_LEFT,
                    AudioFormat.CHANNEL_IN_RIGHT,
                    AudioFormat.CHANNEL_IN_FRONT,
                    AudioFormat.CHANNEL_IN_BACK,
                    AudioFormat.CHANNEL_IN_LEFT_PROCESSED,
                    AudioFormat.CHANNEL_IN_RIGHT_PROCESSED,
                    AudioFormat.CHANNEL_IN_FRONT_PROCESSED,
                    AudioFormat.CHANNEL_IN_BACK_PROCESSED,
                    AudioFormat.CHANNEL_IN_PRESSURE,
                    AudioFormat.CHANNEL_IN_X_AXIS,
                    AudioFormat.CHANNEL_IN_Y_AXIS,
                    AudioFormat.CHANNEL_IN_Z_AXIS,
                    AudioFormat.CHANNEL_IN_VOICE_UPLINK,
                    AudioFormat.CHANNEL_IN_VOICE_DNLINK,
                    AudioFormat.CHANNEL_IN_MONO,
                    AudioFormat.CHANNEL_IN_STEREO}) {
                try {
                    Log.d(TAG, "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;
                        }
                        recorder.release();
                    }
                } catch (Exception e) {
                    Log.e(TAG, rate + "Exception, keep trying.", e);
                }
            }
        }
    }
    return null;
}

但是函数总是返回null,没有可用的参数。大多数组合都通过了bufferSize!= AudioRecord.ERROR_BAD_VALUE条件检查,但在recorder.getState() == AudioRecord.STATE_INITIALIZED中得到false。 我已经尝试重新启动我的设备(MOTO X2),但它没有起作用。我已声明RECORD_AUDIO权限,以下是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.gowear">

    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:name=".RecordActivity"
            android:label="@string/title_activity_record"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

以下是调试日志:

03-08 20:48:58.080 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 3, channel: 16
03-08 20:48:58.091 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1
03-08 20:48:58.092 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
03-08 20:48:58.092 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
03-08 20:48:58.093 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 3, channel: 12
03-08 20:48:58.096 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1
03-08 20:48:58.097 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
03-08 20:48:58.097 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
03-08 20:48:58.097 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 3, channel: 1
03-08 20:48:58.101 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1
03-08 20:48:58.102 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
03-08 20:48:58.102 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
03-08 20:48:58.102 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 2, channel: 16
03-08 20:48:58.105 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1
03-08 20:48:58.106 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
03-08 20:48:58.106 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
03-08 20:48:58.106 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 2, channel: 12
03-08 20:48:58.109 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1
03-08 20:48:58.110 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
03-08 20:48:58.110 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
03-08 20:48:58.110 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 2, channel: 1
03-08 20:48:58.114 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1
03-08 20:48:58.115 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
03-08 20:48:58.115 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
03-08 20:48:58.115 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 1, channel: 16
03-08 20:48:58.115 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 8000 format 0 channelMask 10
03-08 20:48:58.115 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 8000, format 0, channelMask 0x10; status -22
03-08 20:48:58.115 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 1, channel: 12
03-08 20:48:58.116 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 8000 format 0 channelMask c
03-08 20:48:58.116 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 8000, format 0, channelMask 0xc; status -22
03-08 20:48:58.116 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 1, channel: 1
03-08 20:48:58.116 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 8000 format 0 channelMask 10
03-08 20:48:58.116 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 8000, format 0, channelMask 0x10; status -22
03-08 20:48:58.116 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 5, channel: 16
03-08 20:48:58.116 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 8000 format 0x9000000 channelMask 10
03-08 20:48:58.116 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 8000, format 0x9000000, channelMask 0x10; status -22
03-08 20:48:58.116 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 5, channel: 12
03-08 20:48:58.116 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 8000 format 0x9000000 channelMask c
03-08 20:48:58.116 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 8000, format 0x9000000, channelMask 0xc; status -22
03-08 20:48:58.117 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 5, channel: 1
03-08 20:48:58.117 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 8000 format 0x9000000 channelMask 10
03-08 20:48:58.117 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 8000, format 0x9000000, channelMask 0x10; status -22
03-08 20:48:58.117 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 6, channel: 16
03-08 20:48:58.117 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 8000 format 0xa000000 channelMask 10
03-08 20:48:58.117 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 8000, format 0xa000000, channelMask 0x10; status -22
03-08 20:48:58.117 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 6, channel: 12
03-08 20:48:58.117 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 8000 format 0xa000000 channelMask c
03-08 20:48:58.117 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 8000, format 0xa000000, channelMask 0xc; status -22
03-08 20:48:58.118 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 6, channel: 1
03-08 20:48:58.118 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 8000 format 0xa000000 channelMask 10
03-08 20:48:58.118 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 8000, format 0xa000000, channelMask 0x10; status -22
03-08 20:48:58.118 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 4, channel: 16
03-08 20:48:58.121 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1
03-08 20:48:58.123 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
03-08 20:48:58.123 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
03-08 20:48:58.123 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 4, channel: 12
03-08 20:48:58.126 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1
03-08 20:48:58.127 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
03-08 20:48:58.127 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
03-08 20:48:58.127 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 4, channel: 1
03-08 20:48:58.133 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1
03-08 20:48:58.134 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
03-08 20:48:58.134 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
03-08 20:48:58.134 12250-12250/com.example.gowear D/Recorder: Attempting rate 11025Hz, bits: 3, channel: 16
03-08 20:48:58.137 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1
03-08 20:48:58.138 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
03-08 20:48:58.138 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
03-08 20:48:58.139 12250-12250/com.example.gowear D/Recorder: Attempting rate 11025Hz, bits: 3, channel: 12
03-08 20:48:58.141 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1
03-08 20:48:58.149 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
03-08 20:48:58.149 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
03-08 20:48:58.149 12250-12250/com.example.gowear D/Recorder: Attempting rate 11025Hz, bits: 3, channel: 1
03-08 20:48:58.152 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1
03-08 20:48:58.154 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
03-08 20:48:58.154 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
03-08 20:48:58.154 12250-12250/com.example.gowear D/Recorder: Attempting rate 11025Hz, bits: 2, channel: 16
03-08 20:48:58.157 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1
03-08 20:48:58.159 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
03-08 20:48:58.159 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
03-08 20:48:58.159 12250-12250/com.example.gowear D/Recorder: Attempting rate 11025Hz, bits: 2, channel: 12
03-08 20:48:58.161 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1
03-08 20:48:58.163 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
03-08 20:48:58.163 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
03-08 20:48:58.163 12250-12250/com.example.gowear D/Recorder: Attempting rate 11025Hz, bits: 2, channel: 1
03-08 20:48:58.166 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1
03-08 20:48:58.167 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
03-08 20:48:58.167 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
03-08 20:48:58.167 12250-12250/com.example.gowear D/Recorder: Attempting rate 11025Hz, bits: 1, channel: 16
03-08 20:48:58.168 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 11025 format 0 channelMask 10
03-08 20:48:58.168 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 11025, format 0, channelMask 0x10; status -22

我看过很多类似的问题,但没有一个能解决我的问题。是否还有其他解决方案?谢谢!


你的安卓系统是哪个版本? - mapm
@mapm 在Moto新X上的Android 6.0。 - ProtossShuttle
由于您的版本是Marshmallow,可能的解决方案之一是这个:https://dev59.com/45Dea4cB1Zd3GeqPZELR#33625836 - mapm
@mapm 哦,谢谢!我忘记了这个! - ProtossShuttle
解决了你的问题吗? - mapm
@mapm 是的,非常感谢! - ProtossShuttle
1个回答

2

我也遇到了相同的问题,我的解决方案是,如果你使用的是Marshmallow版本,则有一个新的权限系统。要允许设备上的权限,请按照以下步骤操作:

  1. 进入"应用管理器",即"设置->应用程序->应用管理器"。
  2. 找到你的应用程序并选择它,以获取"应用信息"。
  3. 如果权限选项显示为"未允许任何权限",则点击它以打开这些权限。这将使你的应用程序能够正常运行。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接