3

我的应用程序经常出现以下错误:

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()不调用 Activity)

有人有线索吗?

完整日志:

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
4

2 回答 2

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){
于 2012-10-04T14:15:57.553 回答
0

我有类似的问题。不幸的是,我不得不用 AudioRecord 创建多个线程,所以使用while(recorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING){没有帮助:(

最后我挖掘了这个话题:Android AudioRecord - Won't Initialize 2nd time and after audioRecord.release();works fine :)

也许它会在未来帮助某人......

于 2012-10-31T21:07:35.760 回答