AudioRecord:缓冲区溢出? [英] AudioRecord: buffer overflow?

查看:282
本文介绍了AudioRecord:缓冲区溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序记录时,缓冲区溢出。记录在服务中执行。我不知道为什么我从 AudioFlinger 收到此消息。

I'm getting buffer overflow while RECORDING with my app. The recording is performed in a Service. I could not figure out why I'm getting this message from AudioFlinger.

下面我实例化了 AudioRecord 对象并设置其回调。

Below I instantiate the AudioRecord object and set it's callbacks.

bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
aRecorder = new AudioRecord(audioSource, sampleRate, channelConfig, audioFormat, bufferSize);

aRecorder.setRecordPositionUpdateListener(updateListener);

bytesPerSample = bitsPerSample / 8;
int bytesPerFrame = nChannels * bytesPerSample;
framePeriod = bufferSize / bytesPerFrame; // nr of frames that can be kept in a bufferSize dimension    
int result = aRecorder.setPositionNotificationPeriod(framePeriod);    
buffer = new byte[bufferSize];

audioRecord回调:

The audioRecord callback:

private AudioRecord.OnRecordPositionUpdateListener updateListener = new AudioRecord.OnRecordPositionUpdateListener(){
        public void onPeriodicNotification(AudioRecord recorder){
            int result = aRecorder.read(buffer, 0, buffer.length);
        }

        public void onMarkerReached(AudioRecord recorder)
        {}
    };

我怀疑问题与以下内容有关: aRecorder.setPositionNotificationPeriod(framePeriod) ; -可能对于此 bufferSiz e来说期限过长,并且更快(更短)的期限可以解决问题。

I suspect the problem is related to the:aRecorder.setPositionNotificationPeriod(framePeriod); - maybe the period is too big for this bufferSize and a faster(smaller) period will solve the issue.

有人可以告诉我如何摆脱缓冲区溢出吗?

Could someone tells me how to get rid of the buffer overflow?

推荐答案

问题,将AudioRecord的缓冲区大小更改为最小缓冲区大小的2倍。

To fix that issue, change the buffer size of AudioRecord to 2 times the minimum buffer size.

您可以使用 AudioRecord.getMinBufferSize()静态方法。这将为您提供用于当前格式的最小缓冲区大小。

You can use AudioRecord.getMinBufferSize() static method. This will give you the minimum buffer size to use for your current format.

getMinBufferSize()方法的语法为:

The syntax of getMinBufferSize() method is:

public static int getMinBufferSize (
    int sampleRateInHz, int channelConfig, int audioFormat)

任何小于此数字的值都会在创建AudioRecord对象时导致失败。

Anything less than this number will result in failure while creating the AudioRecord object.

您应该一直在减小缓冲区的大小,因此

You should have been reducing the buffer size, so as not to overwhelm the audio subsystem with demands for data.

请记住要使用覆盖的方法( @Override )对于audioRecord回调,如下所示:

Remember to put the overridden methods (@Override) for audioRecord callback as follows:

private AudioRecord.OnRecordPositionUpdateListener updateListener = new AudioRecord.OnRecordPositionUpdateListener(){

        @Override
        public void onPeriodicNotification(AudioRecord recorder){
            int result = aRecorder.read(buffer, 0, buffer.length);
        }

        @Override
        public void onMarkerReached(AudioRecord recorder)
        {}
    };

我建议阅读以下文章: Android录音,第2部分

I recommend to read the post: Android audio recording, part 2

一个您可以尝试的方法是在记录时使用线程,并在记录的字节上使用其他进程,从而避免主UI线程上的过多负担。

One more thing that you could try is to use threads on recording and the other process on the recorded bytes, thus avoiding too much overload on the main UI thread.

开源示例此方法的代码: musicg_android_demo

The open source sample code for this approach: musicg_android_demo

查看此帖子了解更多- android-audiorecord-class-process-live-mic-audio-quickly-set-up-callback-func

Check this post for more - android-audiorecord-class-process-live-mic-audio-quickly-set-up-callback-func

这篇关于AudioRecord:缓冲区溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆