package br.odb;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.util.Log;

import java.util.ArrayList;

/**
 * Created by monty on 15/10/16.
 */
public class SoundSink {

	private ArrayList<AudioTrack> soundPool = new ArrayList<>();
	private ArrayList<byte[]> soundData = new ArrayList<>();

	public int bufferData(  byte[] data, int sampleRate, int channels, int bits ) {
		int key = soundPool.size();

		int formatChannels;
		int formatBits;

		if ( channels == 1 ) {
			formatChannels = AudioFormat.CHANNEL_OUT_MONO;
		} else {
			formatChannels = AudioFormat.CHANNEL_OUT_STEREO;
		}

		if ( bits == 8 ) {
			formatBits = AudioFormat.ENCODING_PCM_8BIT;
		} else {
			formatBits = AudioFormat.ENCODING_PCM_16BIT;
		}


		int bufferSize = AudioTrack.getMinBufferSize(sampleRate, formatChannels, formatBits);

		AudioTrack player = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, formatChannels, formatBits, bufferSize, AudioTrack.MODE_STREAM);

		soundPool.add( player );
		soundData.add( data );

		return key;
	}

	public void play( int id, float volumeLeft, float volumeRight ) {

		byte[] data = soundData.get( id );
		AudioTrack player = soundPool.get( id );

		player.setVolume( ( volumeLeft + volumeRight ) * 0.5f );
		player.play();
		player.write(data, 0, data.length);
	}
}

@@ -1349,13 +1349,31 @@
 ength);%0A
+%09%09player.stop();%0A%0A
 %09%7D%0A%7D%0A

Add a stop after playing the sound, so to keep the AudioTrack free