Best way to stream data from MediaCodec to AudioTrack

DawidLiszkaDawidLiszka PLMember
edited February 2015 in Xamarin.Android

I'm trying to decode a mp3 file and stream it to AudioTrack. It all works fine but causes a lot of GC on the Java side. I've made sure to not allocate memory in my play/stream loop and blame ByteBuffer.Get(byte[], int, int) binding of allocating a temp Java array. Anyone can confirm and/or show a better way of feeding data from MediaCodec to AudioTrack? (I know API 21 introduced AudioTrack.write(ByteBuffer, ...))

Here is what I do:

byte[] audioBuffer = new byte[...];


ByteBuffer codecOutputBuffer = codecOutputBuffers[outputIndex];

// The next line seems to be the source of a lot of GC during playback
codecOutputBuffer.Get(audioBuffer, 0, bufInfo.Size);

audioTrack.Write(audioBuffer, 0, bufInfo.Size);


Sign In or Register to comment.