Getting device's DeviceNumber from MMDeviceCollection

Feb 5, 2013 at 9:06 PM
Hi Mark, et al,

I'm having trouble getting the correct DeviceNumber when iterating devices with MMDeviceCollection.

MMDeviceCollection does not expose the device's DeviceNumber. And using the collection item's index does not establish the correct DeviceNumber (as in the code snippet below).

Any thoughts on how to get a correct device number from a MMDeviceCollection? And I want to use MMDeviceCollection rather than WaveOut.GetCapabilities(deviceId) because I can get the device full name (not truncated) and the device GUID from the MMDeviceCollection.
int deviceCount = WaveOut.DeviceCount;
                MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
                MMDeviceCollection devEnum = DevEnum.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);

                string[] devName = new string[deviceCount];

                int[] deviceNumber = new int[deviceCount];
                int j = 0;
                foreach (var item in devEnum)
                {
                    devName[j] = item.FriendlyName;
                    deviceNumber[j] = j;
                    j++;
                }

                WaveOut outputDeviceWaveOut = new WaveOut();
                outputDeviceWaveOut.DeviceNumber = deviceNumber[2];
Coordinator
Feb 5, 2013 at 11:20 PM
WaveOut and MMDeviceEnumerator use completely different audio APIs. I wouldn't expect the device numbers to match at all.

Mark
Feb 5, 2013 at 11:58 PM
Ok, thanks Mark.
It looks like the only api to associate a device name to the DeviceNumber is the WaveOut.GetCapabilities(deviceId). As you have been telling me. :-)

I looked thru the WAVEOUTCAPS structure. Microsoft doesn't give you much to work with there.

I'll have to cross my fingers that the first 32 chars of the device name are unique for all sound cards -- although that gets iffy when the user has multiple, identical usb soundcard adapters.