Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing the timer and adding more statuses #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions mb_DiscordRichPresence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,24 @@ private void InitialiseDiscord()
private void HandleErrorCallback(int errorCode, string message) { }
private void HandleDisconnectedCallback(int errorCode, string message) { }

private void UpdatePresence(string song, string duration, int position, string state = "Listening to music")
private void UpdatePresence(string song, string duration, int position, PlayState state)
{
DiscordRPC.RichPresence presence = new DiscordRPC.RichPresence();
presence.state = state;
//presence.state = state;
song = Utility.Utf16ToUtf8(song);
presence.details = song.Substring(0, song.Length - 1);
presence.largeImageKey = "musicbee";
long now = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
presence.startTimestamp = now - position;
if (state == PlayState.Playing)
{
presence.state = "Listening to music";
presence.startTimestamp = now - position;
}
else if (state == PlayState.Paused || state == PlayState.Stopped)
{
presence.state = state.ToString();
presence.startTimestamp = 0;
}
// string[] durations = duration.Split(':');
// long end = now + System.Convert.ToInt64(durations[0]) * 60 + System.Convert.ToInt64(durations[1]);
// presence.endTimestamp = end;
Expand Down Expand Up @@ -117,23 +126,27 @@ public void ReceiveNotification(string sourceFileUrl, NotificationType type)
string song = artist + " - " + trackTitle;
if (string.IsNullOrEmpty(artist)) { song = trackTitle; }
// perform some action depending on the notification type
PlayState state = mbApiInterface.Player_GetPlayState();
switch (type)
{
case NotificationType.PluginStartup:
// perform startup initialisation
case NotificationType.PlayStateChanged:
switch (mbApiInterface.Player_GetPlayState())
switch (state)
{
case PlayState.Playing:
UpdatePresence(song, duration, position / 1000);
UpdatePresence(song, duration, position / 1000, state);
break;
case PlayState.Paused:
UpdatePresence(song, duration, 0, state: "Paused");
UpdatePresence(song, duration, 0, state: state);
break;
case PlayState.Stopped:
UpdatePresence(song, duration, 0, state);
break;
}
break;
case NotificationType.TrackChanged:
UpdatePresence(song, duration, 0);
UpdatePresence(song, duration, 0, state);
break;
}
}
Expand Down