gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:00:40 27 4
gpt4 key购买 nike

我正在使用下面的代码找到一个可用的参数组合来创建一个 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权限,下面是 list 文件:

<?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

...

我已经阅读了很多类似的问题,但没有一个能解决我的问题。还有其他解决办法吗?谢谢!

最佳答案

我遇到了同样的问题,所以我的解决方案是,如果您使用的是 Marshmallow 版本,则有一个新的权限系统。要在您的设备中允许权限,请执行以下步骤:

  1. 按照“设置->应用程序->应用程序管理器”进入“应用程序管理器”。
  2. 找到您的应用程序并选择它以获取“应用程序信息”。
  3. 如果权限选项显示"No permissions allowed"然后点击它到switch on these permissions .这将使您的应用程序正常运行。

关于Android - 无法为所有可能的参数组合初始化 AudioRecord,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35849491/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com