Trying to detect microphone connections; using system callback doesn't work

I’m trying to create a callback to detect when a user connects/disconnects a microphone (specifically on an Android device). I read up on documentation and came up with this:

FMOD.SYSTEM_CALLBACK inputDriverChangeCallback;

void Start()
{
	inputDriverChangeCallback = new FMOD.SYSTEM_CALLBACK(EventCallback);
    var type = FMOD.SYSTEM_CALLBACK_TYPE.RECORDLISTCHANGED | FMOD.SYSTEM_CALLBACK_TYPE.DEVICELISTCHANGED | FMOD.SYSTEM_CALLBACK_TYPE.DEVICELOST | FMOD.SYSTEM_CALLBACK_TYPE.DEVICEREINITIALIZE;
    FMODUnity.RuntimeManager.CoreSystem.setCallback(inputDriverChangeCallback, type);
	RuntimeManager.CoreSystem.setCallback(inputDriverChangeCallback, FMOD.SYSTEM_CALLBACK_TYPE.RECORDLISTCHANGED);
}

[AOT.MonoPInvokeCallback(typeof(FMOD.SYSTEM_CALLBACK))]
static FMOD.RESULT EventCallback(IntPtr system, FMOD.SYSTEM_CALLBACK_TYPE type, IntPtr commandData1, IntPtr commandData2, IntPtr userData)
{
	Debug.LogError("11111111111 Callback: ");
	return FMOD.RESULT.OK;
}

However, when I run this on an Android device and connect/disconnect a headset, the callback is not called. I’ve tried all manner of combinations on the variable “type”, but nothing is working.

I’ve tried other methods of detecting connections such as checking the number of input drivers. However, the number always remains at 1 despite me plugging a headset in. These are the functions I’ve tried:

RuntimeManager.CoreSystem.getRecordNumDrivers(out int numInputDrivers, out numConnectedInputDrivers);

RuntimeManager.CoreSystem.getNumDrivers(out int numOutputDrivers);

When I connect a headset, this is the produced output:

0001/01/01 00:00:00.000 -1 -1 Info  --------- beginning of main
2021/10/15 01:11:03.724 21871 22265 Verbose AudioSystem ioConfigChanged() event 1
2021/10/15 01:11:03.745 21871 22265 Verbose AudioSystem ioConfigChanged() event 1
2021/10/15 01:11:03.762 21871 22265 Verbose AudioSystem ioConfigChanged() event 1
2021/10/15 01:11:03.777 21871 22265 Verbose AudioSystem ioConfigChanged() event 1
2021/10/15 01:11:03.791 21871 22265 Verbose AudioSystem ioConfigChanged() event 1
2021/10/15 01:11:03.823 21871 22266 Warn AudioTrack restoreTrack_l(2169): dead IAudioTrack, PCM, creating a new one from obtainBuffer()
2021/10/15 01:11:03.823 21871 22266 Verbose AudioSystem clearAudioConfigCache()
2021/10/15 01:11:03.825 21871 22265 Verbose AudioSystem ioConfigChanged() event 0
2021/10/15 01:11:03.825 21871 22265 Verbose AudioSystem ioConfigChanged() event 0
2021/10/15 01:11:03.825 21871 22265 Verbose AudioSystem ioConfigChanged() event 0
2021/10/15 01:11:03.826 21871 22265 Verbose AudioSystem ioConfigChanged() event 0
2021/10/15 01:11:03.826 21871 22265 Verbose AudioSystem ioConfigChanged() event 0
2021/10/15 01:11:03.826 21871 22265 Verbose AudioSystem ioConfigChanged() event 0
2021/10/15 01:11:03.830 21871 22265 Verbose AudioSystem ioConfigChanged() event 2
2021/10/15 01:11:03.830 21871 22265 Warn AudioSystem ioConfigChanged() closing unknown output 1733
2021/10/15 01:11:03.834 21871 22265 Verbose AudioSystem ioConfigChanged() event 2
2021/10/15 01:11:03.837 21871 22265 Verbose AudioSystem ioConfigChanged() event 0
2021/10/15 01:11:03.838 21871 22265 Verbose AudioSystem ioConfigChanged() event 2
2021/10/15 01:11:03.843 21871 22265 Verbose AudioSystem ioConfigChanged() event 2
2021/10/15 01:11:03.846 21871 22265 Verbose AudioSystem ioConfigChanged() event 2
2021/10/15 01:11:04.042 21871 22265 Verbose AudioSystem ioConfigChanged() event 3
2021/10/15 01:11:04.078 21871 22266 Verbose AudioSystem removeAudioDeviceCallback audioIo 13 portId 2169
2021/10/15 01:11:04.078 21871 22266 Verbose AudioSystem addAudioDeviceCallback audioIo 13 portId 2177
2021/10/15 01:11:04.084 21871 22266 Debug AudioStreamLegacy processCallbackCommon() stream disconnected
2021/10/15 01:11:04.084 21871 22266 Warn AudioStreamLegacy processCallbackCommon() data, stream disconnected
2021/10/15 01:11:04.084 21871 22266 Error AudioTrack processAudioBuffer(2177): EVENT_MORE_DATA requested 3840 bytes but callback returned -1 bytes
2021/10/15 01:11:04.085 21871 22265 Debug AudioStreamLegacy onAudioDeviceUpdate() devId 3 => 2171
2021/10/15 01:11:04.085 21871 22265 Verbose AudioSystem ioConfigChanged() event 0
2021/10/15 01:11:04.085 21871 22265 Verbose AudioSystem ioConfigChanged() event 0
2021/10/15 01:11:04.086 21871 21896 Verbose AudioSystem ioConfigChanged() event 0
2021/10/15 01:11:04.086 21871 21896 Verbose AudioSystem ioConfigChanged() event 8
2021/10/15 01:11:04.086 21871 21896 Verbose AudioSystem ioConfigChanged() AUDIO_CLIENT_STARTED  io 13 port 2177 num callbacks 1
2021/10/15 01:11:04.086 21871 21896 Debug AudioStreamLegacy onAudioDeviceUpdate() devId 2171 => 2171
2021/10/15 01:11:04.105 21871 21946 Debug AAudio AAudioStream_requestStop(s#16) called
2021/10/15 01:11:04.105 21871 21946 Error AAudioStream setState(16) tried to set to 9 but already DISCONNECTED
2021/10/15 01:11:04.105 21871 21946 Verbose AudioTrack stop(2177): prior state:STATE_ACTIVE
2021/10/15 01:11:04.106 21871 21946 Debug   PlayerBase::stop() from IPlayer
2021/10/15 01:11:04.107 21871 21896 Verbose AudioSystem ioConfigChanged() event 5
2021/10/15 01:11:04.111 21871 21896 Verbose AudioSystem ioConfigChanged() event 5
2021/10/15 01:11:04.118 21871 21896 Verbose AudioSystem ioConfigChanged() event 6
2021/10/15 01:11:04.120 21871 21946 Debug AAudio AAudioStream_close(s#16) called ---------------
2021/10/15 01:11:04.120 21871 21946 Verbose AudioSystem removeAudioDeviceCallback audioIo 13 portId 2177
2021/10/15 01:11:04.123 21871 21896 Verbose AudioSystem ioConfigChanged() event 6
2021/10/15 01:11:04.126 21871 21871 Verbose MediaRouter Audio routes updated: AudioRoutesInfo{ type=HEADSET }, a2dp=false
2021/10/15 01:11:04.126 21871 21871 Verbose MediaRouter Selecting route: RouteInfo{ name=Headphones, description=null, status=null, category=RouteCategory{ name=System types=ROUTE_TYPE_LIVE_AUDIO ROUTE_TYPE_LIVE_VIDEO  groupable=false }, supportedTypes=ROUTE_TYPE_LIVE_AUDIO ROUTE_TYPE_LIVE_VIDEO , presentationDisplay=null }
2021/10/15 01:11:04.127 21871 21946 Verbose AudioTrack stop(2177): prior state:STATE_STOPPED
2021/10/15 01:11:04.127 21871 21946 Verbose AudioTrack ~AudioTrack(2177), releasing session id 16937 from 21871 on behalf of 21871
2021/10/15 01:11:04.131 21871 21946 Debug AAudio AAudioStream_close(s#16) returned 0 ---------
2021/10/15 01:11:04.132 21871 21946 Info AAudio AAudioStreamBuilder_openStream() called ----------------------------------------
2021/10/15 01:11:04.132 21871 21946 Info AudioStreamBuilder rate   =      0, channels  = 0, format   = 0, sharing = SH, dir = OUTPUT
2021/10/15 01:11:04.132 21871 21946 Info AudioStreamBuilder device =      0, sessionId = -1, perfMode = 12, callback: OFF with frames = 0
2021/10/15 01:11:04.132 21871 21946 Info AudioStreamBuilder usage  =     14, contentType = 0, inputPreset = 0, allowedCapturePolicy = 0
2021/10/15 01:11:04.132 21871 21946 Debug   PlayerBase::PlayerBase()
2021/10/15 01:11:04.134 21871 21946 Debug AudioStreamTrack open(), request notificationFrames = 0, frameCount = 0
2021/10/15 01:11:04.134 21871 21946 Verbose AudioTrack set(): streamType -1, sampleRate 0, format 0x5, channelMask 0x3, frameCount 0, flags #104, notificationFrames 0, sessionId 0, transferType 3, uid -1, pid -1
2021/10/15 01:11:04.134 21871 21946 Verbose AudioTrack set(): Building AudioTrack with attributes: usage=14 content=2 flags=0x0 tags=[]
2021/10/15 01:11:04.134 21871 21946 Warn AudioTrack createTrack_l(2177): AUDIO_OUTPUT_FLAG_FAST denied by client, not shared buffer and transfer = TRANSFER_SYNC
2021/10/15 01:11:04.139 21871 21946 Warn AudioStreamTrack open() sampleRate changed from 0 to 48000
2021/10/15 01:11:04.140 21871 21946 Verbose AudioSystem addAudioDeviceCallback audioIo 13 portId 2182
2021/10/15 01:11:04.140 21871 21946 Warn AudioStreamTrack open() flags changed from 0x00000104 to 0x00000000
2021/10/15 01:11:04.140 21871 21946 Warn AudioStreamTrack open() perfMode changed from 12 to 10
2021/10/15 01:11:04.140 21871 21896 Verbose AudioSystem ioConfigChanged() event 0
2021/10/15 01:11:04.140 21871 21896 Verbose AudioSystem ioConfigChanged() event 0
2021/10/15 01:11:04.141 21871 21946 Info AAudio AAudioStreamBuilder_openStream() returns 0 = AAUDIO_OK for s#17 ----------------
2021/10/15 01:11:04.141 21871 21946 Debug AAudio AAudioStream_close(s#17) called ---------------
2021/10/15 01:11:04.141 21871 21946 Verbose AudioSystem removeAudioDeviceCallback audioIo 13 portId 2182
2021/10/15 01:11:04.141 21871 21946 Verbose AudioTrack stop(2182): prior state:STATE_STOPPED
2021/10/15 01:11:04.142 21871 21946 Verbose AudioTrack ~AudioTrack(2182), releasing session id 16945 from 21871 on behalf of 21871
2021/10/15 01:11:04.143 21871 21946 Debug AAudio AAudioStream_close(s#17) returned 0 ---------
2021/10/15 01:11:04.144 21871 21946 Debug AudioManager In isBluetoothScoOn(), calling application: com.hg.ohc
2021/10/15 01:11:04.144 21871 21946 Info fmod FMOD::supportsLowLatency                 : Low latency = false, Pro Audio = false, Bluetooth On = false, Acceptable Block Size = true (480)
2021/10/15 01:11:04.144 21871 21946 Info AAudio AAudioStreamBuilder_openStream() called ----------------------------------------
2021/10/15 01:11:04.144 21871 21946 Info AudioStreamBuilder rate   =      0, channels  = 0, format   = 0, sharing = SH, dir = OUTPUT
2021/10/15 01:11:04.144 21871 21946 Info AudioStreamBuilder device =      0, sessionId = -1, perfMode = 12, callback: ON with frames = 0
2021/10/15 01:11:04.144 21871 21946 Info AudioStreamBuilder usage  =     14, contentType = 0, inputPreset = 0, allowedCapturePolicy = 0
2021/10/15 01:11:04.144 21871 21946 Debug   PlayerBase::PlayerBase()
2021/10/15 01:11:04.144 21871 21946 Debug AudioStreamTrack open(), request notificationFrames = -8, frameCount = 0
2021/10/15 01:11:04.144 21871 21946 Verbose AudioTrack set(): streamType -1, sampleRate 0, format 0x5, channelMask 0x3, frameCount 0, flags #104, notificationFrames -8, sessionId 0, transferType 1, uid -1, pid -1
2021/10/15 01:11:04.144 21871 21946 Verbose AudioTrack set(): Building AudioTrack with attributes: usage=14 content=2 flags=0x0 tags=[]
2021/10/15 01:11:04.148 21871 21896 Verbose AudioSystem ioConfigChanged() event 0
2021/10/15 01:11:04.149 21871 21946 Warn AudioStreamTrack open() sampleRate changed from 0 to 48000
2021/10/15 01:11:04.149 21871 21946 Verbose AudioSystem addAudioDeviceCallback audioIo 13 portId 2183
2021/10/15 01:11:04.149 21871 21946 Warn AudioStreamTrack open() flags changed from 0x00000104 to 0x00000004
2021/10/15 01:11:04.149 21871 21896 Verbose AudioSystem ioConfigChanged() event 0
2021/10/15 01:11:04.150 21871 21946 Info AAudio AAudioStreamBuilder_openStream() returns 0 = AAUDIO_OK for s#18 ----------------
2021/10/15 01:11:04.150 21871 21946 Debug AAudio AAudioStream_close(s#18) called ---------------
2021/10/15 01:11:04.150 21871 21946 Verbose AudioSystem removeAudioDeviceCallback audioIo 13 portId 2183
2021/10/15 01:11:04.150 21871 21896 Verbose AudioSystem ioConfigChanged() event 0
2021/10/15 01:11:04.150 21871 21946 Verbose AudioTrack stop(2183): prior state:STATE_STOPPED
2021/10/15 01:11:04.150 21871 21946 Verbose AudioTrack ~AudioTrack(2183), releasing session id 16953 from 21871 on behalf of 21871
2021/10/15 01:11:04.153 21871 21946 Debug AAudio AAudioStream_close(s#18) returned 0 ---------
2021/10/15 01:11:04.153 21871 21946 Info AAudio AAudioStreamBuilder_openStream() called ----------------------------------------
2021/10/15 01:11:04.153 21871 21946 Info AudioStreamBuilder rate   =      0, channels  = 0, format   = 0, sharing = SH, dir = OUTPUT
2021/10/15 01:11:04.153 21871 21946 Info AudioStreamBuilder device =      0, sessionId = -1, perfMode = 12, callback: ON with frames = 0
2021/10/15 01:11:04.153 21871 21946 Info AudioStreamBuilder usage  =     14, contentType = 0, inputPreset = 0, allowedCapturePolicy = 0
2021/10/15 01:11:04.153 21871 21946 Debug   PlayerBase::PlayerBase()
2021/10/15 01:11:04.153 21871 21946 Debug AudioStreamTrack open(), request notificationFrames = 0, frameCount = 9120
2021/10/15 01:11:04.153 21871 21946 Verbose AudioTrack set(): streamType -1, sampleRate 0, format 0x5, channelMask 0x3, frameCount 9120, flags #104, notificationFrames 0, sessionId 0, transferType 1, uid -1, pid -1
2021/10/15 01:11:04.153 21871 21946 Verbose AudioTrack set(): Building AudioTrack with attributes: usage=14 content=2 flags=0x0 tags=[]
2021/10/15 01:11:04.157 21871 21946 Warn AudioStreamTrack open() sampleRate changed from 0 to 48000
2021/10/15 01:11:04.157 21871 21946 Verbose AudioSystem addAudioDeviceCallback audioIo 13 portId 2184
2021/10/15 01:11:04.157 21871 21946 Warn AudioStreamTrack open() flags changed from 0x00000104 to 0x00000004
2021/10/15 01:11:04.158 21871 21896 Verbose AudioSystem ioConfigChanged() event 0
2021/10/15 01:11:04.158 21871 21896 Verbose AudioSystem ioConfigChanged() event 0
2021/10/15 01:11:04.158 21871 21946 Info AAudio AAudioStreamBuilder_openStream() returns 0 = AAUDIO_OK for s#19 ----------------
2021/10/15 01:11:04.160 21871 21946 Debug AAudio AAudioStream_requestStart(s#19) called --------------
2021/10/15 01:11:04.160 21871 21946 Verbose AudioTrack start(2184): prior state:STATE_STOPPED
2021/10/15 01:11:04.161 21871 21946 Debug   PlayerBase::start() from IPlayer
2021/10/15 01:11:04.161 21871 21946 Debug AAudio AAudioStream_requestStart(s#19) returned 0 ---------
2021/10/15 01:11:04.162 21871 21871 Verbose MediaRouter Selecting route: RouteInfo{ name=Headphones, description=null, status=null, category=RouteCategory{ name=System types=ROUTE_TYPE_LIVE_AUDIO ROUTE_TYPE_LIVE_VIDEO  groupable=false }, supportedTypes=ROUTE_TYPE_LIVE_AUDIO ROUTE_TYPE_LIVE_VIDEO , presentationDisplay=null }
2021/10/15 01:11:04.165 21871 21963 Verbose AudioSystem ioConfigChanged() event 0
2021/10/15 01:11:04.166 21871 21963 Verbose AudioSystem ioConfigChanged() event 0
2021/10/15 01:11:04.166 21871 21963 Verbose AudioSystem ioConfigChanged() event 8
2021/10/15 01:11:04.166 21871 21963 Verbose AudioSystem ioConfigChanged() AUDIO_CLIENT_STARTED  io 13 port 2184 num callbacks 1
2021/10/15 01:11:04.166 21871 21963 Debug AudioStreamLegacy onAudioDeviceUpdate() devId 2171 => 2171
2021/10/15 01:11:04.171 21871 22306 Verbose AudioTrack hasStarted(2184): hasStarted wait:1  ets:0  start position:0
2021/10/15 01:11:04.171 21871 22306 Info chatty uid=10250(com.hg.ohc) AudioTrack identical 6 lines
2021/10/15 01:11:04.171 21871 22306 Verbose AudioTrack hasStarted(2184): hasStarted wait:1  ets:0  start position:0
2021/10/15 01:11:04.175 21871 22306 Verbose AudioTrack hasStarted(2184): hasStarted wait:0  ets:3840  start position:0

I can see that something has detected the new connection, but I cannot find the code. Any ideas on how to accurately detect when headphones/headset is connected to the device using FMOD?

I haven’t been able to repro this issue- this looks like how you are supposed to detect mic changes and it worked on my Android device. Is the callback firing when running in the Unity Editor?
Just as a sanity test for the callback, I suggest setting the callback to FMOD.SYSTEM_CALLBACK_TYPE.ALL so it will get all types, and putting a debug in there that outputs the type so you can search through manually and determine if this is a problem with just one type of callback type or all of them. e.g

using UnityEngine;
using FMODUnity;
using System;

public class RecordChangeTest : MonoBehaviour
{
    FMOD.SYSTEM_CALLBACK inputDriverChangeCallback;

    void Start()
    {
        inputDriverChangeCallback = new FMOD.SYSTEM_CALLBACK(EventCallback);
        RuntimeManager.CoreSystem.setCallback(inputDriverChangeCallback, FMOD.SYSTEM_CALLBACK_TYPE.ALL);
    }

    FMOD.RESULT EventCallback(IntPtr system, FMOD.SYSTEM_CALLBACK_TYPE type, IntPtr commandData1, IntPtr commandData2, IntPtr userData)
    {
        Debug.LogWarning("[DBG] " + type.ToString());
        return FMOD.RESULT.OK;
    }

}