Question

I have an Android AudioTrack such as:

private AudioTrack mAudioTrack;
int min = AudioTrack.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT);
mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT, min, AudioTrack.MODE_STREAM);
mAudioTrack.play();

and I fill it with:

mAudioTrack.write(buffer, 0, bufsize);

I would like to have this track to be played with a 2-second delay. Any idea how I could achieve that? Thanks in advance for any good idea.

Was it helpful?

Solution

Here is an answer to my own question in case anybody is interested. The idea is to create a temporary buffer that creates and stores the audio "latency".

First, initialization:

final int mSizeTable = 300;
private byte[][] mTableAudioBuffer = byte[mSizeTable][];
private int mCountAudioBuffer = 0;
...
mAudioTrack.play();

Then, I fill with:

mTableAudioBuffer[mCountAudioBuffer] = buffer.clone();
int ret = 0;
if (mTableAudioBuffer[(mCountAudioBuffer + 1) % mSizeTable] != null)
    ret = mAudioTrack.write(mTableAudioBuffer[(mCountAudioBuffer + 1) % mSizeTable], 0, bufsize);
mCountAudioBuffer = (mCountAudioBuffer + 1) % mSizeTable;
return ret;

Obviously, memory-wise, it's not very optimal and can be greedy if you want a large delay.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top