4

W/AudioRecord(17145): obtainBuffer timed out (is the CPU pegged?) user=11b7f820, server=11b7f820我在录制 1-2 小时后收到。显然,由于 AudioRecord 中的 read() 读取了 0 个字节,因此录制停止了。我为此苦苦挣扎了两个多星期,但尚未找到解决方案。这是我初始化 audioRecord 对象的方法:

private void initPCMRecorder() throws Exception {
        Log.d(getClass().getName(), "Initializing AudioRecord...");
        int channelConfig = nChannels == 1 ? AudioFormat.CHANNEL_IN_MONO: AudioFormat.CHANNEL_IN_STEREO;  
        bufferSize = AudioRecord.getMinBufferSize(sRate, channelConfig, aFormat);
        if (bufferSize == AudioRecord.ERROR_BAD_VALUE)
            Log.e(getClass().getName(), "Bad encoding value, see logcat");                  
        else if (bufferSize == AudioRecord.ERROR)
            Log.e(getClass().getName(), "Error creating buffer size");

        bufferSize *= 3;
        buffer = new byte[bufferSize];

        //check if is not yet released
        if(aRecorder != null){
            try {
                aRecorder.release();    
            } catch (Exception e) {
                Log.e(getClass().getName(), e.getMessage(), e);
            }           
        }

        System.out.println("Source="+aSource+"; sampRate="+sRate+"; format="+aFormat+"; bufSize="+bufferSize);
        aRecorder = new AudioRecord(aSource, sRate, channelConfig, aFormat, bufferSize);
        if (aRecorder.getState() != AudioRecord.STATE_INITIALIZED) {
            throw new Exception("" + ErrorCodes.ERROR_CODE_0);
        }
        Log.d(getClass().getName(), "AudioRecord Initialized");
        state = State.INITIALIZING;
    }

我读了一个辅助线程。请注意我尝试使用 positionNotificationListener,但结果是一样的。

public void run() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

    while (!stopped) {
        while (!suspend) {
            int result = aRecorder.read(buffer, 0, buffer.length);
            if(result == AudioRecord.ERROR_INVALID_OPERATION){
                Log.e(getClass().getName(), "aRecorder.read() returns ERROR_INVALID_OPERATION !!!");
                continue;
            }else if(result == AudioRecord.ERROR_BAD_VALUE){
                Log.e(getClass().getName(), "aRecorder.read() returns ERROR_BAD_VALUE !!!");
                continue;
            }else if(result == 0){
                Log.w(getClass().getName(), "SKIP audio block.");
                continue;
            }

            payloadSize += buffer.length;

            //data processing/writing is done in another thread 
            WAVData audioData = new WAVData(buffer);
            audioData.setGain(rGain);
            audioData.setBitsPerSamples(bitsPerSample);
            audioData.setNrChannels(nChannels);
            if(writersArray.size()>=1)
                writersArray.get(writersArray.size() - 1).add(audioData);
        }
        // lock here
        synchronized (lock) {
            try {
                lock.wait();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                return;
            }
        }
    }
}

编辑(从控制台添加错误 -com.audioRec.android.recorder是我的应用程序):

08-02 12:30:38.162: D/dalvikvm(1451): GC_CONCURRENT freed 7867K, 36% free 18889K/29447K, paused 17ms+8ms, total 151ms
08-02 12:30:38.953: D/dalvikvm(1451): GC_FOR_ALLOC freed 36K, 36% free 18892K/29447K, paused 46ms, total 47ms
08-02 12:30:39.063: D/dalvikvm(1451): GC_CONCURRENT freed <1K, 29% free 21196K/29447K, paused 18ms+6ms, total 108ms
08-02 12:30:59.004: W/AudioRecord(703): obtainBuffer timed out (is the CPU pegged?) user=00f49160, server=00f49160
08-02 12:30:59.515: W/AudioRecord(703): obtainBuffer timed out (is the CPU pegged?) user=00f49160, server=00f49160
08-02 12:31:00.035: W/AudioRecord(703): obtainBuffer timed out (is the CPU pegged?) user=00f49160, server=00f49160
08-02 12:31:00.526: W/com.audioRec.android.recorder.AudioRecorder$RecorderThread(703): SKIP audio block.
08-02 12:31:00.766: W/AudioRecord(703): obtainBuffer timed out (is the CPU pegged?) user=00f49160, server=00f49160
08-02 12:31:01.297: W/AudioRecord(703): obtainBuffer timed out (is the CPU pegged?) user=00f49160, server=00f49160
08-02 12:31:01.797: W/AudioRecord(703): obtainBuffer timed out (is the CPU pegged?) user=00f49160, server=00f49160
08-02 12:31:02.308: W/AudioRecord(703): obtainBuffer timed out (is the CPU pegged?) user=00f49160, server=00f49160
08-02 12:31:02.558: W/com.audioRec.android.recorder.AudioRecorder$RecorderThread(703): SKIP audio block.
08-02 12:31:02.558: D/com.audioRec.android.recorder.AudioRecorder$RecorderThread(703): MUST RESET AUDIORECORD....obtainBuffer timed out ?
08-02 12:31:02.568: D/com.audioRec.android.recorder.AudioRecorder$AudioRecordCalibrator(703): Calibrating AudioRecord object...
08-02 12:31:08.494: E/alsa_pcm(257): Arec: error5
08-02 12:31:08.494: W/AudioStreamInALSA(257): pcm_read() returned error n -5, Recovering from error
08-02 12:31:08.615: E/ALSADevice(257): standby handle h 0x4234f358
08-02 12:31:08.645: D/alsa_ucm(257): snd_use_case_set(): uc_mgr 0x42e24fb8 identifier _dismod value Capture Music
08-02 12:31:08.645: D/alsa_ucm(257): Set mixer controls for Capture Music enable 0
08-02 12:31:08.655: D/alsa_ucm(257): Setting mixer control: MultiMedia1 Mixer SLIM_0_TX, value: 0
08-02 12:31:08.655: E/ALSADevice(257): Number of modifiers 0
08-02 12:31:08.655: E/ALSADevice(257): usecase_type is 0

请帮我解决一下这个。我想到的唯一解决方案是检查 2 个连续读取 0 字节的 read() 并再次停止/启动 audioRecord,但会导致 0.5 秒的录音丢失。

4

1 回答 1

2

已知音频 API 在某些特定设备上被破坏。

我的建议是:

一个。默认执行正常行为(只需继续录制,直到您需要正常停止)。我假设问题仅出现在特定型号上,因此默认行为将适用于大多数用户的设备。

湾。通过识别设备类型Build.MODEL并创建需要解决方法的设备模型字符串数组。例如,将您自己的设备型号添加到列表中,并从用户报告中扩展它(如果有的话)。

C。在您的逻辑中,检查您是否在需要解决方法的设备上运行 - 如果是,请执行启发式方法以识别问题 - 从 AudioRecord 顺序读取 0 字节 - 然后重新启动它。

我发现最好在依赖于设备型号查询的特殊情况下隔离类似的变通方法,以防止您的“正常”代码变脏。

于 2013-08-09T14:15:33.653 回答