我在初始化Android AudioRecord
时遇到了这个问题。我在网上搜索了一段时间没有成功。
对于手机,我在SDK版本7上使用三星GalaxyS。对于AudioRecord
初始化,我使用8000作为采样率,使用MONO进行通道配置,使用16位进行音频格式,并根据对于日志,minBufferSize设置为4160.我已将清单的AUDIO_RECORD权限添加。
我的初始化代码如下:
...
private static int SAMPLE_RATE = 8000;
private static int CHANNEL_CONFIG = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private static int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
// ??? Both 8Bit and Default are deemed illegal.
public MicVolumeManager() {
this.bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE,
CHANNEL_CONFIG, AUDIO_FORMAT);
PhoneDebugger.debug("AUDIO-BUFFER-SIZE",
Integer.toString(this.bufferSize));
this.recorder = new AudioRecord(AudioSource.MIC, SAMPLE_RATE,
CHANNEL_CONFIG, AUDIO_FORMAT, this.bufferSize);
this.audioBuffer = new byte[this.bufferSize];
}
...
但是,对象(this.recorder)无法初始化。以下是使用DDMS的日志:
音频 - 缓冲 - 尺寸(3253): 4160
AudioRecord(3253): set():sampleRate 8000,频道16,frameCount 2080
AudioPolicyManager(2175): getInput()inputSource 1,samplingRate 8000,format 1,channels 10,acoustics 0
AudioFlinger(2175): openInput()openInputStream返回输入0x0,SamplingRate 8000,格式1,通道10,声学0,状态-17
AudioRecord(3253):无法获取记录源1的音频输入 AudioRecord-JNI(3253):创建AudioRecord实例时出错:初始化检查失败。
AudioRecord-Java(3253): [android.media.AudioRecord]错误代码 -20初始化本机AudioRecord对象时。
请帮忙吗?非常感谢!
答案 0 :(得分:16)
对我来说,原因是没有为AudioRecord的前一个实例调用AudioRecord.release();它绑定了AudioFlinger中的本机资源,并干扰了后续的AudioRecord实例。在三星Fascinate(Galaxy S)Android 2.1(Eclair)上看到它; Eclair或三星的实施可能特别不容忍。
答案 1 :(得分:8)
出现同样的错误,直到我重新启动设备。
似乎在我的 Galaxy S 上,本机impl是错误的:多次获取和释放AudioRecorder
(在整个手机正常运行时间内)会导致错误。
答案 2 :(得分:5)
使用录音机后,您必须停止并释放它。然后当你下次启动录音机时,没关系。
答案 3 :(得分:4)
您好我在尝试初始化AudioRecord对象时遇到了同样的问题,我发现解决方案是在实际尝试实例化当前的AudioRecord对象之前测试configurtion参数。这是我用过的粗纱:
/**
* Scan for the best configuration parameter for AudioRecord object on this device.
* Constants value are the audio source, the encoding and the number of channels.
* That means were are actually looking for the fitting sample rate and the minimum
* buffer size. Once both values have been determined, the corresponding program
* variable are initialized (audioSource, sampleRate, channelConfig, audioFormat)
* For each tested sample rate we request the minimum allowed buffer size. Testing the
* return value let us know if the configuration parameter are good to go on this
* device or not.
*
* This should be called in at start of the application in onCreate().
*
* */
public void initRecorderParameters(int[] sampleRates){
for (int i = 0; i < sampleRates.length; ++i){
try {
Log.i(TAG, "Indexing "+sampleRates[i]+"Hz Sample Rate");
int tmpBufferSize = AudioRecord.getMinBufferSize(sampleRates[i],
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
// Test the minimum allowed buffer size with this configuration on this device.
if(tmpBufferSize != AudioRecord.ERROR_BAD_VALUE){
// Seems like we have ourself the optimum AudioRecord parameter for this device.
AudioRecord tmpRecoder = new AudioRecord(MediaRecorder.AudioSource.MIC,
sampleRates[i],
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
tmpBufferSize);
// Test if an AudioRecord instance can be initialized with the given parameters.
if(tmpRecoder.getState() == AudioRecord.STATE_INITIALIZED){
String configResume = "initRecorderParameters(sRates) has found recorder settings supported by the device:"
+ "\nSource = MICROPHONE"
+ "\nsRate = "+sampleRates[i]+"Hz"
+ "\nChannel = MONO"
+ "\nEncoding = 16BIT";
Log.i(TAG, configResume);
//+++Release temporary recorder resources and leave.
tmpRecoder.release();
tmpRecoder = null;
return;
}
}else{
Log.w(TAG, "Incorrect buffer size. Continue sweeping Sampling Rate...");
}
} catch (IllegalArgumentException e) {
Log.e(TAG, "The "+sampleRates[i]+"Hz Sampling Rate is not supported on this device");
}
}
}
我跳这个帮助。
Dickwan
答案 4 :(得分:3)
这也影响了Sansung Galaxy标签GT-P1000和后来的10.1一个
这个错误非常难以重现,重启是我发现逃脱这种糟糕状态的唯一方法......
三星是否有跟踪错误的跟踪器?
答案 5 :(得分:3)
如果你没有获得Record_Audio权限,那么就比你得到错误一样,然后关掉你的手机而不是再次开启然后运行你的应用程序。
答案 6 :(得分:0)
我认为音频硬件最多不能支持10个频道,你设置为输入,所以尝试使用2个频道,看它是否可以正常工作。