public async Task PlayAsync(string deviceUniqueId, string friendlyName) { DevicePlayStateChanged?.Invoke(deviceUniqueId, EState.Playing); CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); if (m_ActivePlayers.ContainsKey(deviceUniqueId) == false) { m_ActivePlayers.Add(deviceUniqueId, cancellationTokenSource); } else { m_ActivePlayers[deviceUniqueId] = cancellationTokenSource; Logger.Error("device {0} already existed in m_ActivePlayers", friendlyName); } try { await _PlayAsync(deviceUniqueId, cancellationTokenSource).ConfigureAwait(false); } catch (NullReferenceException e) { Logger.Error(e, "Error while trying to play, device {0}", friendlyName); Snapcast.Instance.ShowNotification("Device not found", string.Format("Couldn't find sound device '{0}'. Has it been unplugged?", friendlyName)); DevicePlayStateChanged?.Invoke(deviceUniqueId, EState.Stopped); m_ActivePlayers.Remove(deviceUniqueId); if (System.Windows.MessageBox.Show(string.Format("The audio device '{0}' had been marked for auto-play, but is missing from the system. Would you like to remove it from auto-play?", friendlyName), "Snap.Net - Auto-play device missing", System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes) { SnapSettings.SetAudioDeviceAutoPlay(deviceUniqueId, false, friendlyName); } } }
private async Task _ListDevices() { SnapDotNet.Player.Device[] devices = await SnapDotNet.Player.Player.GetDevicesAsync(); m_DeviceControls.Clear(); spDevices.Children.Clear(); foreach (SnapDotNet.Player.Device device in devices) { Controls.Device d = new Controls.Device(device); SnapDotNet.Player.Device dev = device; d.State = m_Player.GetState(dev.UniqueId); d.OnPlayClicked += () => { if (d.State == SnapDotNet.Player.EState.Stopped) { Task play = Task.Run(() => m_Player.PlayAsync(dev.UniqueId, dev.Name)); play.ConfigureAwait(false); } else { m_Player.Stop(dev.UniqueId); } }; d.OnSettingsClicked += () => { DeviceSettings settings = new DeviceSettings(device); settings.ShowDialog(); }; d.OnAutoPlayToggled += (bool autoPlay) => { SnapSettings.SetAudioDeviceAutoPlay(dev.UniqueId, autoPlay, dev.Name); }; bool autoPlay = false; if (m_DeviceAutoPlayFlags.ContainsKey(dev.UniqueId)) { autoPlay = m_DeviceAutoPlayFlags[dev.UniqueId].Item1; } d.SetAutoPlay(autoPlay); spDevices.Children.Add(d); m_DeviceControls.Add(dev.UniqueId, d); } }
public async Task PlayAsync(string deviceUniqueId, string friendlyName) { DevicePlayStateChanged?.Invoke(deviceUniqueId, EState.Playing); CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); if (m_ActivePlayers.ContainsKey(deviceUniqueId) == false) { m_ActivePlayers.Add(deviceUniqueId, cancellationTokenSource); } else { m_ActivePlayers[deviceUniqueId] = cancellationTokenSource; Logger.Error("device {0} already existed in m_ActivePlayers", friendlyName); } try { await _PlayAsync(deviceUniqueId, cancellationTokenSource).ConfigureAwait(false); } catch (NullReferenceException e) { DevicePlayStateChanged?.Invoke(deviceUniqueId, EState.Stopped); m_ActivePlayers.Remove(deviceUniqueId); Logger.Error(e, "Error while trying to play, device {0}", friendlyName); if (SnapSettings.DeviceMissingBehaviour == SnapSettings.EDeviceMissingBehaviour.Default) { Snapcast.Instance.ShowNotification("Device not found", string.Format("Couldn't find sound device '{0}'. Has it been unplugged?", friendlyName)); } else if (SnapSettings.DeviceMissingRetryIntervalSeconds > 0) { Timer timer = new Timer(SnapSettings.DeviceMissingRetryIntervalSeconds * 1000); timer.Start(); timer.Elapsed += async(sender, args) => { timer.Stop(); await PlayAsync(deviceUniqueId, friendlyName); }; } DeviceSettings settings = SnapSettings.GetDeviceSettings(deviceUniqueId); if (settings.LastSeen == null) // in case we're migrating from a version that didn't have the LastSeen field yet { settings.LastSeen = DateTime.Now; SnapSettings.SaveDeviceSettings(deviceUniqueId, settings); } // if a device hasn't been seen for x days, stop trying to auto-play it if (SnapSettings.DeviceMissingExpiryDays != 0) { if (DateTime.Now - settings.LastSeen > new TimeSpan(SnapSettings.DeviceMissingExpiryDays, 0, 0, 0)) { SnapSettings.SetAudioDeviceAutoPlay(deviceUniqueId, false, friendlyName); } } // this prompt needs rethinking - it's only useful when a device has permanently disappeared // maybe add an extra player list at the bottom with "not found" devices? (disable play button etc) //if (System.Windows.MessageBox.Show(string.Format("The audio device '{0}' had been marked for auto-play, but is missing from the system. Would you like to remove it from auto-play?", friendlyName), // "Snap.Net - Auto-play device missing", System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes) //{ // SnapSettings.SetAudioDeviceAutoPlay(deviceUniqueId, false, friendlyName); //} } }