Support USB Audio devices on Android

Android 7.0 (Nougat, API 24) introduced some extra methods to allow you to obtain an AudioRouting object (which will allow you to get the audio device which is currently in use) from OpenSL by using the AcquireJavaProxy method.

Here’s how:

  1. When creating your OpenSL player include the SL_IID_ANDROIDCONFIGURATION interface

  2. Once the player is realized obtain the configuration interface:

    SLresult result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_ANDROIDCONFIGURATION, &configItf);

  3. Use the configuration interface to obtain the AudioRouting handle

    SLresult result = (*configItf)->AcquireJavaProxy(configItf, SL_ANDROID_JAVA_PROXY_ROUTING, &routingObj);

  4. Pass this back to Java (or do some JNI hackery) to obtain an AudioRouting object

  5. Call AudioRouting.getRoutedDevice() to obtain an AudioDeviceInfo object

Example app here: https://android.googlesource.com/platform/cts/+/master/tests/tests/media/libndkaudio

3 Likes