private void SoundDevices_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.NewItems != null) { foreach (var device in e.NewItems.OfType <SoundDevice>()) { var newlyConnectedDevice = KnownSoundDevices.SingleOrDefault(x => x.Id == device.ID); if (newlyConnectedDevice != null) { if (AudioDeviceManager.Instance.SelectedSoundDevice != null && AudioDeviceManager.Instance.SelectedSoundDevice.ID != newlyConnectedDevice.Id) { var actualDevice = KnownSoundDevices.Single(x => x.Id == AudioDeviceManager.Instance.SelectedSoundDevice.ID); if (newlyConnectedDevice.Priority < actualDevice.Priority) { AudioDeviceManager.Instance.SetSelectedSoundDevice(device, false); } } } else { KnownSoundDevices.Add(new BlankSoundDevice() { Description = device.Description, Id = device.ID, Priority = KnownSoundDevices.Count }); SaveKnownDevices(); } } } }
private void SaveKnownDevices() { var json = JsonSerializer.Serialize(KnownSoundDevices.OrderBy(x => x.Priority)); if (!File.Exists(knowDevicesPath)) { var stream = File.Create(knowDevicesPath); stream.Flush(); stream.Close(); } File.WriteAllText(knowDevicesPath, json); }
public override void Initialize() { base.Initialize(); var json = LoadKnownDevices(knowDevicesPath); if (!string.IsNullOrEmpty(json)) { var soundDevices = JsonSerializer.Deserialize <IEnumerable <BlankSoundDevice> >(json); foreach (var soundDevice in soundDevices) { KnownSoundDevices.Add(soundDevice); } } }