private 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")); } }
public static List <SoundOutRepresenter> GetSoundOutList() { List <SoundOutRepresenter> result = new List <SoundOutRepresenter>(); using (MMDeviceEnumerator enumerator = new MMDeviceEnumerator()) { MMDevice standarddevice = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia); if (WasapiOut.IsSupportedOnCurrentPlatform) { var wasApiItem = new SoundOutRepresenter { Name = "WASAPI", SoundOutMode = SoundOutMode.WASAPI }; using ( MMDeviceCollection devices = enumerator.EnumAudioEndpoints(DataFlow.Render, DeviceState.Active)) { wasApiItem.AudioDevices.Add(new AudioDevice("-0", "Windows Default")); wasApiItem.AudioDevices.AddRange( devices.Select( device => new AudioDevice(device.DeviceID, device.FriendlyName, standarddevice.DeviceID == device.DeviceID))); } result.Add(wasApiItem); } var directSoundItem = new SoundOutRepresenter { Name = "DirectSound", SoundOutMode = SoundOutMode.DirectSound }; directSoundItem.AudioDevices.Add(new AudioDevice("-0", "Windows Default")); directSoundItem.AudioDevices.AddRange( new DirectSoundDeviceEnumerator().Devices.Select(x => new AudioDevice(x.Guid.ToString(), x.Description, x.Description == standarddevice.FriendlyName))); result.Add(directSoundItem); return(result); } }
private 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 directSoundDevices = DirectSoundDeviceEnumerator.EnumerateDevices(); var directSoundItem = new SoundOutRepresenter(deviceId => { var device = directSoundDevices .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 directSoundDevices.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; }