java Android AudioRecord无法初始化(其他解决方案不起作用)
今天我已经检查并尝试了所有其他线程好几个小时了,但没有一个解决方案有效
我尝试过过滤所有可用的音频选项。我已授予该应用程序适当的权限
目标:我正在尝试获取此音频流,以便获得音频的频率
我的东西
public int audioSource = MediaRecorder.AudioSource.MIC;
public int channelConfig = AudioFormat.CHANNEL_IN_MONO;
public int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
public AudioRecord audioRecord = null;
private Thread recordingThread = null;
public int blockSize = 256; // deal with this many samples at a time
public int sampleRate = 8000; // Sample rate in Hz
后来
int bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioEncoding);
AudioRecord audioeeeeRecord = new AudioRecord(audioSource, sampleRate, channelConfig, audioEncoding, bufferSize); // The RAW PCM sample recording
audioRecord = audioeeeeRecord;
if (audioRecord != null && audioRecord.getState() != AudioRecord.STATE_INITIALIZED)
try {
throw new Exception("AudioRecord init failed"); //audioRecord = findAudioRecord();
} catch (Exception e) {
e.printStackTrace();
}
final short[] buffer = new short[blockSize];
try {
audioRecord.startRecording();
} catch (Exception e) {
e.printStackTrace();
}
这会引发以下错误(http://pastebin.com/raw/3wmA4cku):
AudioRecord: AudioFlinger could not create record track, status: -1
E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
E/安卓.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
W/System.err: java.lang.Exception: AudioRecord init failed
W/System.err: at app.mobile.mobileecg.MainActivity.startReading(MainActivity.java:102)
W/System.err: at app.mobile.mobileecg.MainActivity$1.onClick(MainActivity.java:58)
W/System.err: at 安卓.view.View.performClick(View.java:5697)
W/System.err: at 安卓.view.View$PerformClick.run(View.java:22526)
W/System.err: at 安卓.os.Handler.handleCallback(Handler.java:739)
W/System.err: at 安卓.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at 安卓.os.Looper.loop(Looper.java:158)
W/System.err: at 安卓.app.ActivityThread.main(ActivityThread.java:7229)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
W/System.err: at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
W/System.err: java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
W/System.err: at 安卓.media.AudioRecord.startRecording(AudioRecord.java:943)
W/System.err: at app.mobile.mobileecg.MainActivity.startReading(MainActivity.java:114)
W/System.err: at app.mobile.mobileecg.MainActivity$1.onClick(MainActivity.java:58)
W/System.err: at 安卓.view.View.performClick(View.java:5697)
W/System.err: at 安卓.view.View$PerformClick.run(View.java:22526)
W/System.err: at 安卓.os.Handler.handleCallback(Handler.java:739)
W/System.err: at 安卓.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at 安卓.os.Looper.loop(Looper.java:158)
W/System.err: at 安卓.app.ActivityThread.main(ActivityThread.java:7229)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
W/System.err: at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
# 1 楼答案
这就是答案。这不是android所拥有的东西,这是愚蠢的
https://stackoverflow.com/a/33769527/6530367
I know this is a very old question but in Android Marshmallow you have to go on "Settings > Apps > Your App > Permissions" and enble Microphone permission.
# 2 楼答案
我认为问题可能是,您以前尝试过将音频资源绑定起来,然后得到了正确的代码,但现在它无法工作,因为没有可用的音频资源。见this SO question。另外,当构造函数返回未初始化的音频记录时,您正在抛出一个异常,但您捕获了自己的异常,这将导致第二个异常(…在未初始化的音频记录上调用…)在日志中可以忽略,因为它是第一次操作的结果
您可能需要在活动中加入一些try/finally部分和/或OnDestroy和/或OnPause方法,以确保录音在不再使用时发布
一旦清除代码就绪,您可能需要关闭应用程序和/或重新启动手机以释放音频资源
当然,你可能让另一个应用程序保持运行,但我对此表示怀疑
# 3 楼答案
步骤1)检查您的清单,以确保在“应用程序”部分下添加录制\u音频权限。e、 g
步骤2)如果您的设备为M或更高版本,则需要通过设置授予权限->;应用程序->;您的应用程序->;权限
步骤3)如果仍然不起作用,请参阅下面的代码段
现在应该可以了