本文介绍了AudioRecord和AudioTrack延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图开发类似的IRIG为Android的aplication,所以第一步是捕捉麦克风输入,并在同一时间玩。

I'm trying to develop an aplication like iRig for android, so the first step is to capture the mic input and play it at the same time.

我有,但问题是,我得到一些延迟,使这个不可用的,如果我开始处理缓冲区恐怕会完全无法使用。

I have it, but the problem is that i get some latency that makes this unusable, and if I start processing the buffer i'm afraid it will get totally unusable.

我用audiorecord和audiotrack是这样的:

I use audiorecord and audiotrack like this:

    new Thread(new Runnable() {
        public void run() {
            while(mRunning){
                mRecorder.read(mBuffer, 0, mBufferSize);
                //Todo: Apply filters here into the buffer and then play it modified
                mPlayer.write(mBuffer, 0, mBufferSize);
                //Log.v("MY AMP","ARA");
            }

而inicialization是这样的:

And the inicialization this way:

// ==================== INITIALIZE ========================= //
public void initialize(){

    mBufferSize = AudioRecord.getMinBufferSize(mHz,
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT);

    mBufferSize2 = AudioTrack.getMinBufferSize(mHz,
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT);

    mBuffer = new byte[mBufferSize];

    Log.v("MY AMP","Buffer size:" + mBufferSize);

    mRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
                mHz,
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT,
                mBufferSize);

    mPlayer = new AudioTrack(AudioManager.STREAM_MUSIC,
                mHz,
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT,
                mBufferSize2,
                AudioTrack.MODE_STREAM);

}

你知道如何获得更快的响应?谢谢!

do you know how to get a faster response?Thanks!

推荐答案

Android的AudioTrack \ AudioRecord类具有很高的延迟,由于最小缓冲区大小。之所以这些缓冲区大小是尽量减少降低时GC的根据谷歌(这在我看来是一个错误的决定,您可以优化自己的内存管理)的发生。

Android's AudioTrack\AudioRecord classes have high latency due to minimum buffer sizes.The reason for those buffer sizes is to minimize drops when GC's occur according to Google (which is a wrong decision in my opinion, you can optimize your own memory management).

您想要做的是使用OpenSL,可从2.3。它包含了音频流本地API。下面是一些文档:http://mobilepearls.com/labs/native-android-api/opensles/index.html

What you want to do is use OpenSL, which is available from 2.3. It contains native APIs for streaming audio.Here's some docs:http://mobilepearls.com/labs/native-android-api/opensles/index.html

这篇关于AudioRecord和AudioTrack延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 14:40
查看更多