/// <summary> /// Handles checking for playing status changes and pushing out presence updates /// </summary> /// <param name="sender">Sender of this event</param> /// <param name="e">Args of this event</param> private void Timer_OnTick(object sender, EventArgs e) { try { if (_iTunes == null) { _iTunes = new iTunesApp(); } if (_iTunes.CurrentTrack == null) { DiscordRpc.ClearPresence(); return; } } catch (COMException) { _iTunes = null; var newPresence = new DiscordRpc.RichPresence { largeImageKey = "itunes_logo_big", details = "Error connecting to iTunes", state = "Playback information unavailable" }; DiscordRpc.UpdatePresence(newPresence); return; } if (_currentArtist == _iTunes.CurrentTrack.Artist && _currentTitle == _iTunes.CurrentTrack.Name && _currentState == _iTunes.PlayerState && _currentPosition == _iTunes.PlayerPosition) { return; } _currentArtist = _iTunes.CurrentTrack.Artist; _currentTitle = _iTunes.CurrentTrack.Name; _currentState = _iTunes.PlayerState; _currentPosition = _iTunes.PlayerPosition; var presence = new DiscordRpc.RichPresence { largeImageKey = "itunes_logo_big" }; if (_currentState != ITPlayerState.ITPlayerStatePlaying) { presence.details = TruncateString(RenderString(Settings.Default.PausedTopLine)); presence.state = TruncateString(RenderString(Settings.Default.PausedBottomLine)); } else { presence.details = TruncateString(RenderString(Settings.Default.PlayingTopLine)); presence.state = TruncateString(RenderString(Settings.Default.PlayingBottomLine)); if (Settings.Default.DisplayPlaybackDuration) { presence.startTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds() - _currentPosition; presence.endTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds() + (_iTunes.CurrentTrack.Duration - _currentPosition); } } try { DiscordRpc.UpdatePresence(presence); } catch (Exception exception) { exception.Data.Add("CurrentArtist", _currentArtist); exception.Data.Add("CurrentTitle", _currentTitle); exception.Data.Add("CurrentState", _currentState.ToString()); exception.Data.Add("CurrentPosition", _currentPosition.ToString()); exception.Data.Add("Details", presence.details); exception.Data.Add("State", presence.state); Globals.RavenClient.Capture(new SentryEvent(exception)); Globals.Log($"An unhandled exception has occurred and been reported to Sentry: {exception.Message}"); } }
/// <summary> /// Handles checking for playing status changes and pushing out presence updates /// </summary> /// <param name="sender">Sender of this event</param> /// <param name="e">Args of this event</param> private void Timer_OnTick(object sender, EventArgs e) { try { if (ITunes == null) { ITunes = new iTunesApp(); } if (ITunes.CurrentTrack == null || (Settings.Default.ClearOnPause && ITunes.PlayerState != ITPlayerState.ITPlayerStatePlaying)) { DiscordRpc.ClearPresence(); return; } } catch (COMException) { ITunes = null; var newPresence = new DiscordRpc.RichPresence { largeImageKey = "itunes_logo_big", details = "Error connecting to iTunes", state = "Playback information unavailable" }; DiscordRpc.UpdatePresence(newPresence); return; } catch (EntryPointNotFoundException) { var newPresence = new DiscordRpc.RichPresence { largeImageKey = "itunes_logo_big", details = "No song playing", state = "Re-install iTunesRichPresence to clear this message" }; DiscordRpc.UpdatePresence(newPresence); return; } if (_currentArtist == ITunes.CurrentTrack.Artist && _currentTitle == ITunes.CurrentTrack.Name && _currentState == ITunes.PlayerState && _currentPosition == ITunes.PlayerPosition) { return; } _currentArtist = ITunes.CurrentTrack.Artist; _currentTitle = ITunes.CurrentTrack.Name; _currentState = ITunes.PlayerState; _currentPosition = ITunes.PlayerPosition; int _currentAlbumCoverCount = ITunes.CurrentTrack.Artwork.Count; string PREFIX = "itlps-"; foreach (var artT in ITunes.CurrentTrack.Artwork) { var art = artT as IITArtwork; string ext = ".tmp"; switch (art.Format) { case ITArtworkFormat.ITArtworkFormatBMP: ext = ".bmp"; break; case ITArtworkFormat.ITArtworkFormatJPEG: ext = ".jpg"; break; case ITArtworkFormat.ITArtworkFormatPNG: ext = ".png"; break; } string path = Path.Combine(Path.GetTempPath(), PREFIX + Path.GetRandomFileName() + ext); art.SaveArtworkToFile(path); //Debug.WriteLine(path); } Debug.WriteLine("DB ID: " + ITunes.CurrentTrack.TrackDatabaseID); Debug.WriteLine("Track ID: " + ITunes.CurrentTrack.trackID); Debug.WriteLine("Artwork: " + ITunes.CurrentTrack.Artwork); var presence = new DiscordRpc.RichPresence { largeImageKey = path }; if (_currentState != ITPlayerState.ITPlayerStatePlaying) { presence.details = TruncateString(RenderString(Settings.Default.PausedTopLine)); presence.state = TruncateString(RenderString(Settings.Default.PausedBottomLine)); } else { presence.details = "[" + _currentArtist + "](https://google.co.uk)"; //Embed top line presence.state = _currentTitle; //Embed bottom line. if (Settings.Default.DisplayPlaybackDuration) { presence.startTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds() - _currentPosition; presence.endTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds() + (ITunes.CurrentTrack.Duration - _currentPosition); } } try { DiscordRpc.UpdatePresence(presence); } catch (Exception exception) { exception.Data.Add("CurrentArtist", _currentArtist); exception.Data.Add("CurrentTitle", _currentTitle); exception.Data.Add("CurrentState", _currentState.ToString()); exception.Data.Add("CurrentPosition", _currentPosition.ToString()); exception.Data.Add("Details", presence.details); exception.Data.Add("State", presence.state); Globals.RavenClient.Capture(new SentryEvent(exception)); Globals.Log($"An unhandled exception has occurred and been reported to Sentry: {exception.Message}"); } }