我正在尝试实现一个监听麦克风输入(具体来说是呼吸)并基于其显示数据的应用程序。我正在使用Android类AudioRecord,但在尝试实例化AudioRecord时出现了三个错误。
AudioRecord: AudioFlinger could not create record track, status: -1
AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
我发现了这个非常棒的帖子:AudioRecord对象未初始化 我借鉴了被接受的答案中尝试所有采样率、音频格式和通道配置来解决问题的代码,但没有帮助,无论哪种设置都会出现上述错误。 我还根据线程中的一个答案在几个地方添加了对AudioRecord.release()的调用,但没有任何区别。
这是我的代码:
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.util.Log;
public class SoundMeter {
private AudioRecord ar = null;
private int minSize;
private static int[] mSampleRates = new int[] { 8000, 11025, 22050, 32000, 44100 };
public boolean start() {
ar = findAudioRecord();
if(ar != null){
ar.startRecording();
return true;
}
else{
Log.e("SoundMeter", "ERROR, could not create audio recorder");
return false;
}
}
public void stop() {
if (ar != null) {
ar.stop();
ar.release();
}
}
public double getAmplitude() {
short[] buffer = new short[minSize];
ar.read(buffer, 0, minSize);
int max = 0;
for (short s : buffer)
{
if (Math.abs(s) > max)
{
max = Math.abs(s);
}
}
return max;
}
public AudioRecord findAudioRecord() {
for (int rate : mSampleRates) {
for (short audioFormat : new short[] { AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT, AudioFormat.ENCODING_PCM_FLOAT }) {
for (short channelConfig : new short[] { AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO }) {
try {
Log.d("SoundMeter", "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
Log.d("SoundMeter", "Found a supported bufferSize, attempting to instantiate");
AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize);
if (recorder.getState() == AudioRecord.STATE_INITIALIZED){
minSize = bufferSize;
return recorder;
}
else
recorder.release();
}
} catch (Exception e) {
Log.e("SoundMeter", rate + " Exception, keep trying.", e);
}
}
}
}
return null;
}
我还添加了
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
根据此线程中其他答案的建议,将标签添加到我的清单文件中,作为 manifest 标记的子级和 application 标记的同级。在添加该标签后,我已经重新构建了项目。
当我在 Nexus 5 手机(不是模拟器)上调试时,我发现了这些解决方案,但不幸的是它们似乎对我没有用。在调用 AudioRecord 的构造函数时出现这些错误。我已经多次重启手机以尝试释放麦克风,但无济于事。该项目基于 Android 4.4,并且我的手机当前运行的是 Android 6.0.1。
请问还有什么我可以尝试的,或者我可能错过了什么?谢谢!