lebhero
lebhero

Reputation: 1441

order of the playback devices returned from DirectSoundEnumerate

In my application I want to give the user the chance to choose which sound device he wants to use for playing a given mp3 file. using

[DllImport("dsound.dll", EntryPoint = "DirectSoundEnumerateA", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        static extern void DirectSoundEnumerate(DSEnumCallback lpDSEnumCallback, IntPtr lpContext);

private static List<DirectSoundDeviceInfo> devices;
var devicesList = new List<DirectSoundDeviceInfo>();
DirectSoundEnumerate(new DSEnumCallback(EnumCallback), IntPtr.Zero);

iam getting a list of all the sound devices installed on my pc. smthg like:

{Primary Sound Driver, Speakers (Realtek High Definition Audio), realtek sound card channel (5), realtek sound card channel (2), realtek sound card channel (1), realtek sound card channel (3), realtek sound card channel (4)}

if call the method PlaySound(4); it will play the mp3 on realtek sound card channel (1)

now to play the mp3 file, iam using naudio.

public void PlaySound(int deviceNumber)
        {
            //disposeWave();// stop previous sounds before starting
            var waveReader = new NAudio.Wave.Mp3FileReader("Kalimba.mp3");
            var waveOut = new NAudio.Wave.WaveOut();
            waveOut.DeviceNumber = deviceNumber;
            var output = waveOut;
            output.Init(waveReader);
            output.Play();
        }

the problem is: the order of the sound devices inside the devicesList is not the same as the devices order in windows ..

the order of the devices under windows is: {Speakers (Realtek High Definition Audio), realtek sound card channel (1), realtek sound card channel (2), realtek sound card channel (3), realtek sound card channel (4), realtek sound card channel (5)}

and if call the method PlaySound(4); it will play the mp3 on realtek sound card channel (4)

so if I choose the DeviceNumber = 1 (referring to the 2nd device in my deviceList) it is not the same device that has index 1 in windows ..

my question is: how can I sort the devices in my devicelist, so that they match the same sort under windows so that I can choose the correct sound device from my list? how are the sound devices sorted usually ?

any help is really appreciated ..

thanks in advance

Upvotes: 1

Views: 1247

Answers (1)

Ansis Māliņš
Ansis Māliņš

Reputation: 1712

Why are you using DllImport when you have NAudio??

DirectSound devices are identified by GUIDs, not indexes. Their order is indeterminate, and you can't sort them.

http://mark-dot-net.blogspot.de/2011/05/naudio-audio-output-devices.html

To select a specific device with DirectSound, you can call the static DirectSoundOut.Devices property which will let you get at the GUID for each device, which you can pass into the DirectSoundOut constructor.

Upvotes: 1

Related Questions