void CheckDefaultAudioDevice(SoundOutRepresenter representer) { if (representer.AudioDevices.All(x => x.ID == DefaultDevicePlaceholder)) { representer.AudioDevices.Clear(); //No default item if there are no other devices } else if(representer.AudioDevices.Count > 0 && representer.AudioDevices.All(x => x.ID != DefaultDevicePlaceholder)) { representer.AudioDevices.Insert(0, new AudioDevice("-0", "Windows Default")); } }
void RefreshSoundOutRepresenter() { var result = new ObservableCollection<SoundOutRepresenter>(); using (var enumerator = new MMDeviceEnumerator()) { MMDevice standarddevice; try { standarddevice = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia); } catch (CoreAudioAPIException) { standarddevice = null; } if (WasapiOut.IsSupportedOnCurrentPlatform) { var wasApiItem = new SoundOutRepresenter(deviceId => { using (var mmdeviceEnumerator = new MMDeviceEnumerator()) { var device = mmdeviceEnumerator.EnumAudioEndpoints(DataFlow.Render, DeviceState.Active) .FirstOrDefault(x => x.DeviceID == deviceId); return device == null ? null : new AudioDevice(device.DeviceID, device.FriendlyName); } }) { Name = "WASAPI", SoundOutMode = SoundOutMode.WASAPI }; using (var devices = enumerator.EnumAudioEndpoints(DataFlow.Render, DeviceState.Active)) { foreach (var device in devices.Select(device => new AudioDevice(device.DeviceID, device.FriendlyName, standarddevice != null && standarddevice.DeviceID == device.DeviceID))) { wasApiItem.AudioDevices.Add(device); } } CheckDefaultAudioDevice(wasApiItem); result.Add(wasApiItem); } var directSoundItem = new SoundOutRepresenter(deviceId => { var device = new DirectSoundDeviceEnumerator().Devices .FirstOrDefault(x => x.Guid.ToString() == deviceId); return device == null ? null : new AudioDevice(device.Guid.ToString(), device.Description); }) { Name = "DirectSound", SoundOutMode = SoundOutMode.DirectSound }; foreach (var device in new DirectSoundDeviceEnumerator().Devices.Select(x => new AudioDevice(x.Guid.ToString(), x.Description, standarddevice != null && x.Description == standarddevice.FriendlyName))) { directSoundItem.AudioDevices.Add(device); } CheckDefaultAudioDevice(directSoundItem); result.Add(directSoundItem); } SoundOutList = result; }