简体   繁体   English

Android - 无法为所有可能的参数组合初始化AudioRecord

[英]Android - Failed to Initialize an AudioRecord for All Possible Combinations of Arguments

I'm using the code below find an available combination of params to create an AudioRecord object. 我正在使用下面的代码找到一个可用的params组合来创建一个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;
}

But the function always returns null , with no available params found. 但是函数总是返回null ,没有找到可用的参数。 Most combinations pass the bufferSize != AudioRecord.ERROR_BAD_VALUE condition check, but get false for recorder.getState() == AudioRecord.STATE_INITIALIZED . 大多数组合通过bufferSize != AudioRecord.ERROR_BAD_VALUE条件检查,但得到falserecorder.getState() == AudioRecord.STATE_INITIALIZED

I have tried to reboot my device (MOTO X2), but it didn't work. 我试图重新启动我的设备(MOTO X2),但它没有用。

I have declared the RECORD_AUDIO permission, and below is the manifest file: 我已经声明了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>

Below is the debug log: 以下是调试日志:

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

... ...

I have read lots of similar questions but none of them could fix my problem. 我已经阅读了很多类似的问题,但没有一个可以解决我的问题。 Is there any other solution? 还有其他解决方案吗? Thanks! 谢谢!

I had the same problem so my solution was, if you are using the Marshmallow version there is a new permission system. 我有同样的问题所以我的解决方案是,如果您使用Marshmallow版本,则有一个新的权限系统。 To allow the permissions in your device do this steps: 要允许设备中的权限,请执行以下步骤:

  1. Go to "Application manager" as follow "Settings->Applications->Application manager". 转到“应用程序管理器”,如下所示“设置 - >应用程序 - >应用程序管理器”。
  2. Locate your application a select it to get the "Application info". 找到您的应用程序,选择它以获取“应用程序信息”。
  3. If the permissions options shows "No permissions allowed" then tap on it to switch on these permissions . 如果权限选项显示“无权限允许”,则点击它以打开这些权限 This will allow your app to run fine. 这将使您的应用程序运行正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM