Android Oboe, AudioDeviceManger and USB devices

Hi guys.

I am trying to properly handle USB audio devices connections with Oboe.
I did apply the stabilised callback mentioned in other posts, plumbed a JNI notification from a custom JavaApp class using AudioManager. registerAudioDeviceCallback function.

What I am not really sure is how to make my juce’s AudioDeviceManager react properly.

Thanks!

Did you manage to get this to work properly?

I have the same question. I need notifications of added/removed USB devices.

Here’s a patch to support the Oboe AudioIODeviceType::Listener notification:


diff --git a/modules/juce_audio_devices/native/java/app/com/rmsl/juce/JuceAudioDevicesListener.java b/modules/juce_audio_devices/native/java/app/com/rmsl/juce/JuceAudioDevicesListener.java
new file mode 100644
index 0000000000..651f0a8490
--- /dev/null
+++ b/modules/juce_audio_devices/native/java/app/com/rmsl/juce/JuceAudioDevicesListener.java
@@ -0,0 +1,52 @@
+package com.rmsl.juce;
+
+import android.content.Context;
+
+import android.media.AudioManager;
+import android.media.AudioDeviceInfo;
+import android.media.AudioDeviceCallback;
+
+
+public final class JuceAudioDevicesListener extends AudioDeviceCallback
+{
+    // first callback is triggered by registration
+    boolean triggerCallback = false;
+    long audioDeviceType = 0;
+
+    private native void handleDevicesChanged (long adt);
+
+    @Override
+    public void onAudioDevicesAdded (AudioDeviceInfo[] addedDevices)
+    {
+        if (triggerCallback)
+        {
+            if (0 != audioDeviceType)
+            {
+                handleDevicesChanged (audioDeviceType);
+            }
+        }
+        else
+        {
+            triggerCallback = true;
+        }
+    }
+
+    @Override
+    public void onAudioDevicesRemoved (AudioDeviceInfo[] removedDevices)
+    {
+        if (0 != audioDeviceType)
+        {
+            handleDevicesChanged (audioDeviceType);
+        }
+    }
+
+    public JuceAudioDevicesListener (Context context, long delegate)
+    {
+        audioDeviceType = delegate;
+
+        AudioManager audioManager = (AudioManager) context.getSystemService (
+                Context.AUDIO_SERVICE);
+
+        audioManager.registerAudioDeviceCallback (this, null);
+    }
+}
\ No newline at end of file
diff --git a/modules/juce_audio_devices/native/juce_Oboe_android.cpp b/modules/juce_audio_devices/native/juce_Oboe_android.cpp
index 0ee468f287..7ec728077e 100644
--- a/modules/juce_audio_devices/native/juce_Oboe_android.cpp
+++ b/modules/juce_audio_devices/native/juce_Oboe_android.cpp
@@ -45,6 +45,85 @@
 namespace juce
 {
 
+const uint8 javaJuceAudioDevicesListener []
+{
+  0x1f, 0x8b, 0x08, 0x08, 0x39, 0x62, 0x45, 0x69, 0x00, 0x03, 0x63, 0x6c,
+  0x61, 0x73, 0x73, 0x65, 0x73, 0x2e, 0x64, 0x65, 0x78, 0x00, 0x75, 0x54,
+  0x5d, 0x6f, 0x1b, 0x45, 0x14, 0xbd, 0x33, 0xbb, 0x9b, 0xa4, 0x1f, 0x38,
+  0xa9, 0x9b, 0x84, 0x86, 0x16, 0x61, 0x6d, 0x25, 0x44, 0xa5, 0xd8, 0x6b,
+  0xa7, 0x29, 0xb6, 0x63, 0x84, 0x88, 0x52, 0x24, 0xb0, 0xb6, 0x02, 0x91,
+  0x92, 0x87, 0x52, 0x45, 0x1d, 0xef, 0x4e, 0x9c, 0x4d, 0xd7, 0xbb, 0xee,
+  0xae, 0x63, 0xb9, 0x42, 0x54, 0x6d, 0x9f, 0x22, 0x24, 0x24, 0x90, 0xf8,
+  0xa8, 0x78, 0xa0, 0x82, 0x07, 0x84, 0x80, 0x1f, 0xd0, 0x87, 0x3e, 0xf0,
+  0xd0, 0xb7, 0xfe, 0x85, 0x22, 0xfe, 0x41, 0x9f, 0xf8, 0x01, 0x3d, 0xb3,
+  0x3b, 0x49, 0x5c, 0xa9, 0xb1, 0x7c, 0xf6, 0xdc, 0xb9, 0x77, 0xee, 0xcc,
+  0xb9, 0xd7, 0xeb, 0xeb, 0xcb, 0xd1, 0xf1, 0xea, 0xc5, 0x26, 0x15, 0x27,
+  0x9d, 0xe2, 0xbd, 0xfa, 0xe6, 0x1f, 0x17, 0xe6, 0xc5, 0xf7, 0x9b, 0xb7,
+  0xfe, 0xfd, 0x3d, 0xf9, 0xab, 0xb2, 0xfa, 0x53, 0xd8, 0xf8, 0xf3, 0xb1,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c0, 0x62, 0x7a, 0x06, 0xbf, 0x00,
+  0xb5, 0x40, 0x48, 0x13, 0xbc, 0x05, 0x00, 0x00
+};
+
+
 template <typename OboeDataFormat>  struct OboeAudioIODeviceBufferHelpers {};
 
 template <>
@@ -1064,6 +1143,13 @@ public:
     OboeAudioIODeviceType()
         : AudioIODeviceType (OboeAudioIODevice::oboeTypeName)
     {
+        // the listener must register before the audio manager is accessed
+        audioDevicesListener = GlobalRef (
+                LocalRef<jobject> (getEnv ()->NewObject (
+                        JuceAudioDevicesListener,
+                        JuceAudioDevicesListener.create, getAppContext().get(),
+                        (jlong)this)));
+
         // Not using scanForDevices() to maintain behaviour backwards compatible with older APIs
         checkAvailableDevices();
     }
@@ -1133,11 +1219,29 @@ public:
        #endif
     }
 
+    static void handleDevicesChangedCallback (JNIEnv*,
+            OboeAudioIODeviceType& type)
+    {
+        type.checkAvailableDevices ();
+
+        type.callDeviceChangeListeners ();
+    }
+
+#define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
+ METHOD   (create, "<init>", "(Landroid/content/Context;J)V") \
+ CALLBACK (generatedCallback<&OboeAudioIODeviceType::handleDevicesChangedCallback>, "handleDevicesChanged", "(J)V")
+
+DECLARE_JNI_CLASS_WITH_BYTECODE (JuceAudioDevicesListener, "com/rmsl/juce/JuceAudioDevicesListener", 29, javaJuceAudioDevicesListener)
+#undef JNI_CLASS_MEMBERS
+
  private:
     void checkAvailableDevices()
     {
         auto sampleRates = OboeAudioIODevice::getDefaultSampleRates();
 
+        inputDevices.clearQuick ();
+        outputDevices.clearQuick ();
+
         inputDevices .add ({ "System Default (Input)",  oboe::kUnspecified, sampleRates, 1 });
         outputDevices.add ({ "System Default (Output)", oboe::kUnspecified, sampleRates, 2 });
 
@@ -1313,6 +1417,8 @@ public:
 
     Array<DeviceInfo> inputDevices, outputDevices;
 
+    GlobalRef audioDevicesListener;
+
     JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OboeAudioIODeviceType)
 };
 

1 Like