我经常在我的应用上收到以下错误:
09-25 15:52:24.875: E/AudioHardware(144): -----AudioStreamInALSA::read(0x42db0040, 512) END ERROR
09-25 15:52:24.875: E/AudioFlinger(144): Error reading audio input
09-25 15:52:24.882: D/AudioHardware(144): AudioStreamInALSA::setParameters() routing=0
09-25 15:52:25.394: W/AudioRecord(7359): obtainBuffer timed out (is the CPU pegged?) user=00000000, server=00000000
09-25 15:52:25.398: D/AudioHardware(144): AudioStreamInALSA::setParameters() input_source=1;routing=262144
我创建AudioRecord实例的代码是:
int minValue = AudioRecord.getMinBufferSize(sampleRate,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
// Gets a buffer size that is greater than the minimun required and is multiple of the chunk size
BUFFER_SIZE_IN_BYTES = getBufferSizeInChunks(minValue);
AudioRecord audioRecord = new AudioRecord(
MediaRecorder.AudioSource.MIC,
sampleRate,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT ,
BUFFER_SIZE_IN_BYTES );
AudioRecord实例如下:
@Override
public void run() {
try{
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
recorder = initAudioRecord();
if (recorder.getState() != AudioRecord.STATE_INITIALIZED) {
return;
}
// Audio input buffer
byte[] audio_data = new byte[BUFFER_SIZE_IN_BYTES];
while (!Thread.interrupted() && keepRunning) {
recorder.startRecording();
recorder.read(audio_data, 0, BUFFER_SIZE_IN_BYTES);
recorder.stop();
// Here I deal with the audio_data ...
// UI feedback
PostToUI(frequency);
}
}
catch(Exception e){
e.printStackTrace();
}
}
private void PostToUI(final double frequency) {
handler.post(new Runnable() {
public void run() {
uiHandler.updateUi(frequency);
}
});
}
分离的线程会不断处理音频输入,如图所示(只要未调用活动的onPause()
)
有人有一些线索吗?
完整日志:
09-27 14:05:29.246: D/AudioHardware(144): AudioStreamInALSA::setParameters() input_source=1;routing=262144
09-27 14:05:29.246: V/AudioHardware(144): openMixer_l() mMixerOpenCnt: 0
09-27 14:05:29.246: V/AudioHardware(144): setInputSource_l(1)
09-27 14:05:29.246: V/AudioHardware(144): closeMixer_l() mMixerOpenCnt: 1
09-27 14:05:29.246: D/AudioHardware(144): AudioHardware pcm capture is exiting standby.
09-27 14:05:29.246: V/AudioHardware(144): AudioStreamInALSA exit standby mNeedEchoReference 0 mEchoReference 0x0
09-27 14:05:29.246: V/AudioHardware(144): open pcm_in driver
09-27 14:05:29.312: V/AudioHardware(144): openMixer_l() mMixerOpenCnt: 0
09-27 14:05:29.312: V/AudioHardware(144): read() wakeup setting route Main Mic
09-27 14:05:41.593: D/Finsky(12273): [1] 5.onFinished: Installation state replication succeeded.
09-27 14:05:49.332: W/AudioHardware(144): read error: -1
09-27 14:05:49.332: D/AudioHardware(144): AudioHardware pcm capture is going to standby.
09-27 14:05:49.332: V/AudioHardware(144): closeMixer_l() mMixerOpenCnt: 1
09-27 14:05:49.375: E/AudioHardware(144): -----AudioStreamInALSA::read(0x42b2d040, 512) END ERROR
09-27 14:05:49.375: E/AudioFlinger(144): Error reading audio input
09-27 14:05:49.386: D/AudioHardware(144): AudioStreamInALSA::setParameters() routing=0
09-27 14:05:49.906: W/AudioRecord(13013): obtainBuffer timed out (is the CPU pegged?) user=00000000, server=00000000
09-27 14:05:49.910: D/AudioHardware(144): AudioStreamInALSA::setParameters() input_source=1;routing=262144
09-27 14:05:49.914: V/AudioHardware(144): openMixer_l() mMixerOpenCnt: 0
09-27 14:05:49.914: V/AudioHardware(144): setInputSource_l(1)
09-27 14:05:49.914: V/AudioHardware(144): closeMixer_l() mMixerOpenCnt: 1
09-27 14:05:49.921: D/AudioHardware(144): AudioHardware pcm capture is exiting standby.
09-27 14:05:49.921: V/AudioHardware(144): AudioStreamInALSA exit standby mNeedEchoReference 0 mEchoReference 0x0
09-27 14:05:49.921: V/AudioHardware(144): open pcm_in driver
09-27 14:05:50.000: V/AudioHardware(144): openMixer_l() mMixerOpenCnt: 0
答案 0 :(得分:2)
有什么理由,为什么你不断打开和关闭录音机? 我会这样说的:
// Audio input buffer
byte[] audio_data = new byte[BUFFER_SIZE_IN_BYTES];
recorder.startRecording();
while (!Thread.interrupted() && keepRunning) {
recorder.read(audio_data, 0, BUFFER_SIZE_IN_BYTES);
// Here I deal with the audio_data ...
// UI feedback
PostToUI(frequency);
}
recorder.stop();
我可以想象,录音机可以提供的不只是缓冲区大小,但你总是只读取那个字节数。
在我的应用程序中,我有方法从run()方法之外启动和停止录制器(我认为你设置keepRunning类似)。在其他地方停止录音时,你 只需像这样调整你的while循环:
while(recorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING){
答案 1 :(得分:0)
我有类似的问题。不幸的是我不得不用AudioRecord创建多个线程,因此使用while(recorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING){
没有帮助:(
最后我挖掘了这个话题:Android AudioRecord - Won't Initialize 2nd time并在audioRecord.release();
之后正常工作:)
将来可能会帮助别人......