filoe / cscore

An advanced audio library, written in C#. Provides tons of features. From playing/recording audio to decoding/encoding audio streams/files to processing audio data in realtime (e.g. applying custom effects during playback, create visualizations,...). The possibilities are nearly unlimited.
Other
2.15k stars 451 forks source link

Trying to understand how to open a Wasapi device if I already know the friendly name or device id #427

Closed ToxMox closed 3 years ago

ToxMox commented 3 years ago

How would I go about setting a Wasapi device based on its Device ID? For example my device id for a specific audio device is {0.0.1.00000000}.{bddbb7cc-a684-4668-ac06-23c2a059ed03} In the snippet of code below I'm able to activate the default microphone. Any idea how would I adjust this code to force the device above?

                    MMDevice defaultMicrophone;
                    DeviceId = deviceId;
                    using (var deviceEnumerator = new MMDeviceEnumerator())
                    {
                        defaultMicrophone = deviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Communications);
                      }
                    _wasapiCapture = new WasapiCapture();
                    _wasapiCapture.Device = defaultMicrophone;
filoe commented 3 years ago

Use the MMDeviceEnumerator to enumerate all capture devices instead of just the default one. The returned devices all have some properties including their DeviceId. Choose the wanted device based on the DeviceId and pass it to the Device property of the WasapiCapture instance as you already done.

ToxMox commented 3 years ago

Thank you that helped!