AudioRecord在1-2小时后停止录制

时间:2013-08-01 08:10:43

标签: android audiorecord android-audiorecord

我在录制1-2小时后收到W/AudioRecord(17145): obtainBuffer timed out (is the CPU pegged?) user=11b7f820, server=11b7f820。显然,自AudioRecord的read()读取0字节后,录制停止。我正在努力解决这个问题超过2周,还没有找到解决方案。以下是我初始化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次read()读取0字节并再次停止/启动audioRecord,但会导致0.5秒的录音丢失。

1 个答案:

答案 0 :(得分:1)

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

我的建议是:

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

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

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

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