public static Song GetCurrentSong() { if (!Spotify.IsRunning()) { return(null); } IntPtr hWnd = GetSpotify(); int length = Win32.GetWindowTextLength(hWnd); StringBuilder sb = new StringBuilder(length + 1); Win32.GetWindowText(hWnd, sb, sb.Capacity); 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); var song = (portions.Length > 1 ? portions[1] : null); var artist = portions[0]; var album = (portions.Length > 2 ? string.Join(" ", portions.Skip(2).ToArray()) : null); // take everything else that's left return(new Song(artist, song, album)); } return(null); }
public static string GetCurrentTrack() { if (!Spotify.IsAvailable()) { return(string.Empty); } IntPtr hWnd = GetSpotify(); int length = Win32.GetWindowTextLength(hWnd); StringBuilder sb = new StringBuilder(length + 1); Win32.GetWindowText(hWnd, sb, sb.Capacity); return(sb.ToString().Replace("Spotify", "").TrimStart(' ', '-').Trim()); }