public ISoundOut GetNewSoundSource() { var settings = HurricaneSettings.Instance.Config; MMDevice defaultDevice; using (var enumerator = new MMDeviceEnumerator()) { try { defaultDevice = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia); } catch (CoreAudioAPIException) { defaultDevice = null; } } if (settings.SoundOutMode == SoundOutMode.DirectSound) { var enumerator = new DirectSoundDeviceEnumerator(); if (enumerator.Devices.Count == 0) { settings.SoundOutMode = SoundOutMode.WASAPI; return GetNewSoundSource(); } DirectSoundDevice device; if (settings.SoundOutDeviceID == DefaultDevicePlaceholder) { device = defaultDevice != null ? enumerator.Devices.FirstOrDefault(x => x.Description == defaultDevice.FriendlyName) ?? enumerator.Devices.First() : enumerator.Devices.First(); } else { device = enumerator.Devices.FirstOrDefault(x => x.Guid.ToString() == settings.SoundOutDeviceID); if (device == null) { settings.SoundOutDeviceID = DefaultDevicePlaceholder; return GetNewSoundSource(); } } _currentDeviceId = device.Guid.ToString(); return new DirectSoundOut { Device = device.Guid, Latency = settings.Latency }; } else { using (var enumerator = new MMDeviceEnumerator()) { using (var devices = enumerator.EnumAudioEndpoints(DataFlow.Render, DeviceState.Active)) { if (defaultDevice == null || devices.Count == 0) { settings.SoundOutMode = SoundOutMode.DirectSound; return GetNewSoundSource(); } MMDevice device; if (settings.SoundOutDeviceID == DefaultDevicePlaceholder) { device = defaultDevice; } else { device = devices.FirstOrDefault(x => x.DeviceID == settings.SoundOutDeviceID); if (device == null) { settings.SoundOutDeviceID = DefaultDevicePlaceholder; return GetNewSoundSource(); } } _currentDeviceId = device.DeviceID; return new WasapiOut { Device = device, Latency = settings.Latency }; } } } }
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; }