-
-
Save martindevans/59bf1202ace94186807dab53dbf5444c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CanBeNull] | |
private static AudioDevice ChooseAudioDevice([CanBeNull] string name) | |
{ | |
var criterion = MediaDeviceCriteria.AudioDevice; | |
var query = new MediaDeviceQuery(criterion); | |
var useDefault = string.IsNullOrWhiteSpace(name); | |
AudioDevice firstDevice = null; | |
for (var i = 0; i < query.count; i++) | |
{ | |
// Skip invalid devices | |
if (!(query[i] is AudioDevice audioDevice)) | |
continue; | |
// Keep track of the first valid device, in case we don't find anything better | |
if (firstDevice == null) | |
firstDevice = audioDevice; | |
// If the default has been requested then use the default as soon as we find it | |
if (useDefault && audioDevice.defaultForMediaType) | |
return audioDevice; | |
// Use the device if the name matches | |
if (!useDefault && audioDevice.name.Equals(name, StringComparison.InvariantCultureIgnoreCase)) | |
return audioDevice; | |
} | |
// None of the device match the name, use the first available device | |
return firstDevice; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment