AudioTrack makes clicking noise every second in Android app

770 Views Asked by At

I have the following code which reads objects from a queue that contain PCM data and writes the PCM data to an Android audio

@Override
public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_AUDIO);
    try {
        Thread.sleep(3000L);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
            44100,
            AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT,
            AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT),
            AudioTrack.MODE_STREAM);
    keepPlaying.set(true);
    audioTrack.play();
    while (keepPlaying.get()) {
        if (!decodedAudioQueue.isEmpty()) {
            DecodedTimedAudioChunk chunk = decodedAudioQueue.poll();
            byte[] arr = chunk.getPcmData();
            audioTrack.write(arr, 0, arr.length);
            continue;
        } else {
            Log.e(TAG, "NO audio in queue to play! ");
            continue;
        }

Each decodedaudiochunk has a byte array that is 88,200 in length. The audio is 16 bit mono @ 44100hz.

THe problem is I hear a "popping" noise every second. The music is still coherent but the popping makes my app seem broken to end-users.

What could cause this popping noise? It seems to occur once per DecodedAudioChunk.

0

There are 0 best solutions below