Java – Notification messages from the AudioTrack stream

Notification messages from the AudioTrack stream… here is a solution to the problem.

Notification messages from the AudioTrack stream

I have implemented a PCM decoder that writes to an AudioTrack object.

Everything seems fine, however, I need to get some kind of notification from the AudioTrack object when the last written block stops playing.

I’ve noticed that there are callback methods such as setNotificationMarkerPosition, however, I can’t find any exhaustive documentation on how to use them.

Thanks in advance!

Solution

To get an AudioTrack callback, you can set up a tag callback or use a periodic callback. I’ve seen reports of tagging issues, so you might want to try both.

For tag callbacks, first call setNotificationMarkerPosition and use whatever frame number you want to call.

For periodic callbacks, call setPositionNotificationPeriod It will call every x frames.

Either way, you’ll need to call setPlaybackPositionUpdateListener to register the callback. This calls two methods, onMarkerReached if the marker is reached, or onPeriodicNotification per set number of frames. You can choose to use one or the other, or both. Both callbacks reference the instance of AudioTrack that you used to set it up.

By default, it will callback in the same thread that created the AudioTrack instance. Alternatively, you can pass a handler to send it to another thread when registering the callback.

Related Problems and Solutions