public static void SendAction(SpotifyAction a) { // bah. Because control cannot fall through cases we need to special case volume if (SettingsXml.Current.ChangeSpotifyVolumeOnly) { if (a == SpotifyAction.VolumeUp) { VolumeHelper.IncrementVolume("Spotify"); return; } else if (a == SpotifyAction.VolumeDown) { VolumeHelper.DecrementVolume("Spotify"); return; } else if (a == SpotifyAction.Mute) { VolumeHelper.ToggleApplicationMute("Spotify"); return; } } switch (a) { case SpotifyAction.CopyTrackInfo: case SpotifyAction.ShowToast: //Nothing break; case SpotifyAction.ShowSpotify: ShowSpotify(); break; } }
public static Song GetCurrentSong() { if (!Spotify.IsRunning()) { return(null); } string song = ""; string artist = ""; string album = ""; IntPtr hWnd = GetSpotify(); int length = Win32.GetWindowTextLength(hWnd); StringBuilder sb = new StringBuilder(length + 1); Win32.GetWindowText(hWnd, sb, sb.Capacity); //float spotifyVolume = VolumeHelper.GetSpotifyInstaVolume(); string title = sb.ToString(); if (!string.IsNullOrWhiteSpace(title) && title != "Spotify") { // Unfortunately we don't have a great way to get the title from Spotify // so we need to do some gymnastics. // Music played from an artist's page is usually in the format "artist - song" // while music played from a playlist is often in the format "artist - song - album" // unfortunately this means that some songs that actually have a " - " in either the artist name // or in the song name will potentially display incorrectly var portions = title.Split(new string[] { " - " }, StringSplitOptions.None); song = (portions.Length > 1 ? portions[1] : null); artist = portions[0]; album = (portions.Length > 2 ? string.Join(" ", portions.Skip(2).ToArray()) : null); // take everything else that's left return(new Song(artist, song, album)); } else if (title == "Spotify") { float spotifyVolume = VolumeHelper.GetSpotifyInstaVolume(); if (!float.IsNaN(spotifyVolume) && spotifyVolume > 0.00001f) { // Most Probably an ad return(new Song("SpotifyAds", "SpotifyAds", "SpotifyAds")); } } return(null); }
public static void SendAction(SpotifyAction a) { if (!Spotify.IsRunning()) { return; } // bah. Because control cannot fall through cases we need to special case volume if (SettingsXml.Current.ChangeSpotifyVolumeOnly) { if (a == SpotifyAction.VolumeUp) { Telemetry.TrackEvent(TelemetryCategory.Action, Telemetry.TelemetryEvent.Action.VolumeUp); VolumeHelper.IncrementVolume("Spotify"); return; } else if (a == SpotifyAction.VolumeDown) { Telemetry.TrackEvent(TelemetryCategory.Action, Telemetry.TelemetryEvent.Action.VolumeDown); VolumeHelper.DecrementVolume("Spotify"); return; } else if (a == SpotifyAction.Mute) { Telemetry.TrackEvent(TelemetryCategory.Action, Telemetry.TelemetryEvent.Action.Mute); VolumeHelper.ToggleApplicationMute("Spotify"); return; } } switch (a) { case SpotifyAction.CopyTrackInfo: case SpotifyAction.ShowToast: //Nothing break; case SpotifyAction.ShowSpotify: Telemetry.TrackEvent(TelemetryCategory.Action, Telemetry.TelemetryEvent.Action.ShowSpotify); if (Spotify.IsMinimized()) { ShowSpotify(); } else { Minimize(); } break; case SpotifyAction.FastForward: Telemetry.TrackEvent(TelemetryCategory.Action, Telemetry.TelemetryEvent.Action.FastForward); SendComplexKeys("+{Right}"); break; case SpotifyAction.Rewind: Telemetry.TrackEvent(TelemetryCategory.Action, Telemetry.TelemetryEvent.Action.Rewind); SendComplexKeys("+{Left}"); break; default: Telemetry.TrackEvent(TelemetryCategory.Action, Telemetry.TelemetryEvent.Action.Default + a.ToString()); Win32.SendMessage(GetSpotify(), Win32.Constants.WM_APPCOMMAND, IntPtr.Zero, new IntPtr((long)a)); break; } }
private void CheckTitle() { Song currentSong = Spotify.GetCurrentSong(); if (currentSong != null && currentSong.IsValid() && !currentSong.Equals(this.currentSong) && !currentSong.IsAnAd()) { // set the previous title asap so that the next timer call to this function will // fail fast (setting it at the end may cause multiple web requests) if (adsFlag) { adsFlag = false; VolumeHelper.SetApplicationMute("Spotify", false); //Debug.WriteLine("UnMuted-Ad finish"); } this.currentSong = currentSong; //Debug.WriteLine(currentSong.ToString()); try { Spotify.SetCoverArt(currentSong); } catch { // Exceptions will be handled (for telemetry etc.) within SetCoverArt, but they will be rethrown // so that we can set custom artwork here currentSong.CoverArtUrl = ALBUM_ACCESS_DENIED_ICON; } // Toastify-specific custom logic around album art (if it's missing, or an ad) UpdateSongForToastify(currentSong); toastIcon = currentSong.CoverArtUrl; this.Dispatcher.Invoke((Action) delegate { Title1.Text = currentSong.Track; Title2.Text = currentSong.Artist; }, System.Windows.Threading.DispatcherPriority.Normal); foreach (var p in this.Plugins) { try { p.TrackChanged(currentSong.Artist, currentSong.Track); } catch (Exception) { //For now we swallow any plugin errors. } } this.Dispatcher.Invoke((Action) delegate { FadeIn(); }, System.Windows.Threading.DispatcherPriority.Normal); if (SettingsXml.Current.SaveTrackToFile) { if (!string.IsNullOrEmpty(SettingsXml.Current.SaveTrackToFilePath)) { try { string trackText = GetClipboardText(currentSong); File.WriteAllText(SettingsXml.Current.SaveTrackToFilePath, trackText); } catch { } // ignore errors writing out the album } } } else if (currentSong != null && currentSong.IsAnAd() && !adsFlag) { adsFlag = true; VolumeHelper.SetApplicationMute("Spotify", true); Debug.WriteLine("Muted-Probably an Ad"); } }