How can I find the hardware audio playback latency of a certain device?

588 views
Skip to first unread message

ilan shiber

unread,
Jun 15, 2015, 12:19:50 PM6/15/15
to android-...@googlegroups.com
Hi everyone,

I am playing audio and video streams that need to be in sync. After playing around with several android devices, I realized that each of them has a different audio playback latency.

I am using AudioTrack and and ran some tests. It seems that even if audiotrack.getPlaybackHeadPosition() is called by the two devices in exactly the same millisecond (to be precise, there might be a 2ms difference) and even if audiotrack.getPlaybackHeadPosition()  returns exactly the same result (i.e. the audiotrack head is positioned on the same frame, at the same time in both devices), still the audio that comes out of one device is always slightly ahead of the audio which comes out of the second device.

It also seems that the results are in line with the latency value I get when running the following code:

        AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        try{
            Method m = am.getClass().getMethod("getOutputLatency", int.class);
            Integer latency = (Integer)m.invoke(am, AudioManager.STREAM_MUSIC);
            mLogger.error(" reported latency is " + latency.intValue());
        }catch(Exception e){
            mLogger.error(" could not determine latency");
        }

In other words - the audio which comes out of the devices which report a greater latency, is lagging.

The problem is that the above code does not run on all android devices. Any idea how I can directly extract the above "hardware latency value" without being dependent on the exact device model and android version?

Thanks!
Ilan



Glenn Kasten

unread,
Jun 15, 2015, 1:52:26 PM6/15/15
to android-...@googlegroups.com, ilans...@gmail.com
Please see http://developer.android.com/reference/android/media/AudioTrack.html#getTimestamp(android.media.AudioTimestamp)
This can be used to get a more accurate measurement of latency, especially for playback to local (on-device) transducer.

ilan shiber

unread,
Jun 16, 2015, 10:08:05 AM6/16/15
to android-...@googlegroups.com, ilans...@gmail.com
Thanks Glenn!

Going to test that one. Yet, it is only available on API level 19 or later:( while I am looking for a way to get this information from older devices (e.g. API 16 and up). A small note - using NDK is acceptable for me, as long as no rooting is required.

Ilan
Reply all
Reply to author
Forward
0 new messages