diff --git a/NowPlayingKiosk/SpotifyNowPlayingTrackInfoProvider.cs b/NowPlayingKiosk/SpotifyNowPlayingTrackInfoProvider.cs index 8d7f496..fedf4e2 100644 --- a/NowPlayingKiosk/SpotifyNowPlayingTrackInfoProvider.cs +++ b/NowPlayingKiosk/SpotifyNowPlayingTrackInfoProvider.cs @@ -5,35 +5,49 @@ namespace NowPlayingKiosk { public class SpotifyNowPlayingTrackInfoProvider : NowPlayingTrackInfoProvider { + private TrackInfo lastPlayed; + public TrackInfo WhatIsNowPlaying() { // Get the process Process[] processList = Process.GetProcessesByName("spotify"); - string windowTitle = null; string wantedPrefix = "Spotify - "; foreach (Process process in processList) { - if (!String.IsNullOrEmpty(process.MainWindowTitle) && process.MainWindowTitle.StartsWith(wantedPrefix)) + if (!String.IsNullOrEmpty(process.MainWindowTitle) && process.MainWindowTitle.Equals("Spotify")) { - windowTitle = process.MainWindowTitle.Substring(wantedPrefix.Length); + // Nothing playing right now + lastPlayed = null; + return null; } - } + else if (!String.IsNullOrEmpty(process.MainWindowTitle) && process.MainWindowTitle.StartsWith(wantedPrefix)) + { + // Spotify is playing something + string windowTitle = process.MainWindowTitle.Substring(wantedPrefix.Length); + TrackInfo nowPlaying = null; + if (windowTitle != null) + { + // For example "Foo Fighters – Monkey Wrench" + int idx = windowTitle.IndexOf('–'); + if (idx != -1) + { + string artist = windowTitle.Substring(0, idx).Trim(); + string title = windowTitle.Substring(idx + 1).Trim(); + nowPlaying = new TrackInfo(artist, title); + } + } - TrackInfo nowPlaying = null; - if (windowTitle != null) - { - // For example "Foo Fighters – Monkey Wrench" - int idx = windowTitle.IndexOf('–'); - if (idx != -1) + lastPlayed = nowPlaying; + return nowPlaying; + } + else if (String.IsNullOrEmpty(process.MainWindowTitle)) { - string artist = windowTitle.Substring(0, idx).Trim(); - string title = windowTitle.Substring(idx + 1).Trim(); - nowPlaying = new TrackInfo(artist, title); + return lastPlayed; } } - return nowPlaying; + return null; } } }