1
0

Compare commits

..

9 Commits

Author SHA1 Message Date
0e2fa5f81a Add Timeleft in track 2024-02-22 08:33:46 +01:00
7478741ba2 nullReference 2024-02-22 05:54:41 +01:00
b1a3d2685c simple interpolation 2024-02-22 05:52:55 +01:00
5601f76124 Better playbackstate 2024-02-22 05:51:14 +01:00
364c1e9ee5 Update Readme 2024-02-22 04:00:31 +01:00
1b6d562ee8 wrong index... 2024-02-22 03:57:19 +01:00
e62e6a4af0 Default Presence 2024-02-22 03:55:10 +01:00
b87cd50b0c Remove unnecessary check if mediaSession is focused 2024-02-22 03:54:40 +01:00
5b08f9138b Fix crash when object not ready 2024-02-22 03:46:19 +01:00
3 changed files with 74 additions and 26 deletions

View File

@ -18,24 +18,33 @@ public class DisMediaRP : IDisposable
private readonly ILogger? _logger; private readonly ILogger? _logger;
private readonly MediaManager _mediaManager = new(); private readonly MediaManager _mediaManager = new();
private readonly DiscordRpcClient _discordRpcClient; private readonly DiscordRpcClient _discordRpcClient;
private readonly RichPresence _currentStatus = new() private RichPresence _currentStatus;
{
Assets = new()
{
LargeImageText = "C9Glax/DiscordMediaRichPresence",
SmallImageText = "https://www.flaticon.com/de/autoren/alfanz"
}
};
private bool _running = true; private bool _running = true;
public DisMediaRP(string applicationId, LogLevel? logLevel, string? largeImageKey = null) : this(applicationId, new Logger(logLevel ?? LogLevel.Information), largeImageKey) private static RichPresence DefaultPresence(string largeImageKey)
{
return new RichPresence()
{
Details = "hewwo :3",
State = "https://github.com/C9Glax/DiscordMediaRichPresence",
Assets = new()
{
LargeImageKey = largeImageKey,
SmallImageKey = "music",
LargeImageText = "C9Glax/DiscordMediaRichPresence",
SmallImageText = "https://www.flaticon.com/de/autoren/alfanz"
}
};
}
public DisMediaRP(string applicationId, LogLevel? logLevel, string largeImageKey = "cat") : this(applicationId, new Logger(logLevel ?? LogLevel.Information), largeImageKey)
{ {
} }
public DisMediaRP(string applicationId, ILogger? logger = null, string? largeImageKey = null) public DisMediaRP(string applicationId, ILogger? logger = null, string largeImageKey = "cat")
{ {
this._logger = logger; this._logger = logger;
this._currentStatus.Assets.LargeImageKey = largeImageKey ?? "cat"; this._currentStatus = DefaultPresence(largeImageKey);
this._discordRpcClient = new DiscordRpcClient(applicationId, logger: new DisLogger(this._logger)); this._discordRpcClient = new DiscordRpcClient(applicationId, logger: new DisLogger(this._logger));
this._discordRpcClient.Initialize(); this._discordRpcClient.Initialize();
this._discordRpcClient.OnError += (sender, args) => this._discordRpcClient.OnError += (sender, args) =>
@ -48,14 +57,32 @@ public class DisMediaRP : IDisposable
this._mediaManager.OnAnyMediaPropertyChanged += MediaPropertyChanged; this._mediaManager.OnAnyMediaPropertyChanged += MediaPropertyChanged;
this._mediaManager.OnAnyPlaybackStateChanged += PlaybackStateChanged; this._mediaManager.OnAnyPlaybackStateChanged += PlaybackStateChanged;
this._mediaManager.OnAnyTimelinePropertyChanged += TimelinePropertyChanged; this._mediaManager.OnAnyTimelinePropertyChanged += TimelinePropertyChanged;
this._mediaManager.OnFocusedSessionChanged += mediaSession =>
{
if (mediaSession is null)
{
this._currentStatus = DefaultPresence(largeImageKey);
}
this._discordRpcClient.SetPresence(this._currentStatus);
};
if (this._mediaManager.GetFocusedSession() is not null) if (this._mediaManager.GetFocusedSession() is not null)
{ {
this.MediaPropertyChanged(this._mediaManager.GetFocusedSession(), this._mediaManager.GetFocusedSession().ControlSession.TryGetMediaPropertiesAsync().GetResults()); try
{
this.MediaPropertyChanged(this._mediaManager.GetFocusedSession(),
this._mediaManager.GetFocusedSession().ControlSession.TryGetMediaPropertiesAsync().GetResults());
}
catch (System.Runtime.InteropServices.COMException e)
{
this._logger?.LogError("Could not fetch MediaProperties\n{e}", e);
}
this.PlaybackStateChanged(this._mediaManager.GetFocusedSession(), this._mediaManager.GetFocusedSession().ControlSession.GetPlaybackInfo()); this.PlaybackStateChanged(this._mediaManager.GetFocusedSession(), this._mediaManager.GetFocusedSession().ControlSession.GetPlaybackInfo());
this.TimelinePropertyChanged(this._mediaManager.GetFocusedSession(), this._mediaManager.GetFocusedSession().ControlSession.GetTimelineProperties()); this.TimelinePropertyChanged(this._mediaManager.GetFocusedSession(), this._mediaManager.GetFocusedSession().ControlSession.GetTimelineProperties());
} }else
this._discordRpcClient.SetPresence(this._currentStatus);
while(_running) while(_running)
Thread.Sleep(50); Thread.Sleep(50);
@ -65,9 +92,6 @@ public class DisMediaRP : IDisposable
{ {
this._logger?.LogDebug(ObjectToString(mediaSession)); this._logger?.LogDebug(ObjectToString(mediaSession));
this._logger?.LogDebug(ObjectToString(mediaProperties)); this._logger?.LogDebug(ObjectToString(mediaProperties));
if (mediaSession != this._mediaManager.GetFocusedSession())
return;
string details = $"{mediaProperties.Title}"; string details = $"{mediaProperties.Title}";
if (mediaProperties.Artist.Length > 0) if (mediaProperties.Artist.Length > 0)
@ -76,16 +100,13 @@ public class DisMediaRP : IDisposable
details += $" - Album: {mediaProperties.AlbumTitle}"; details += $" - Album: {mediaProperties.AlbumTitle}";
this._currentStatus.Details = details; this._currentStatus.Details = details;
this._discordRpcClient.SetPresence(this._currentStatus); this.PlaybackStateChanged(mediaSession, mediaSession.ControlSession.GetPlaybackInfo());
} }
private void PlaybackStateChanged(MediaManager.MediaSession mediaSession, GlobalSystemMediaTransportControlsSessionPlaybackInfo playbackInfo) private void PlaybackStateChanged(MediaManager.MediaSession mediaSession, GlobalSystemMediaTransportControlsSessionPlaybackInfo playbackInfo)
{ {
this._logger?.LogDebug(ObjectToString(mediaSession)); this._logger?.LogDebug(ObjectToString(mediaSession));
this._logger?.LogDebug(ObjectToString(playbackInfo)); this._logger?.LogDebug(ObjectToString(playbackInfo));
if (mediaSession != this._mediaManager.GetFocusedSession())
return;
string playbackState = playbackInfo.PlaybackStatus switch string playbackState = playbackInfo.PlaybackStatus switch
{ {
@ -114,14 +135,32 @@ public class DisMediaRP : IDisposable
{ {
this._logger?.LogDebug(ObjectToString(mediaSession)); this._logger?.LogDebug(ObjectToString(mediaSession));
this._logger?.LogDebug(ObjectToString(timelineProperties)); this._logger?.LogDebug(ObjectToString(timelineProperties));
if (mediaSession != this._mediaManager.GetFocusedSession())
return;
if (timelineProperties.LastUpdatedTime < DateTimeOffset.UnixEpoch) if (timelineProperties.LastUpdatedTime < DateTimeOffset.UnixEpoch)
return; return;
GlobalSystemMediaTransportControlsSessionPlaybackInfo playbackInfo =
mediaSession.ControlSession.GetPlaybackInfo();
this._currentStatus.Timestamps = new Timestamps(DateTime.Now.Subtract(timelineProperties.Position), string? repeatMode = playbackInfo.AutoRepeatMode switch
DateTime.Now.Add(timelineProperties.EndTime - timelineProperties.Position)); {
MediaPlaybackAutoRepeatMode.Track => "\ud83d\udd02",
MediaPlaybackAutoRepeatMode.List => "\ud83d\udd01",
_ => null
};
string? shuffle = (playbackInfo.IsShuffleActive ?? false) ? "\ud83d\udd00" : null;
this._currentStatus.State = string.Join(' ', repeatMode, shuffle, $"{timelineProperties.Position:hh\\:mm\\:ss}/{timelineProperties.EndTime:hh\\:mm\\:ss}");
if (mediaSession.ControlSession.GetPlaybackInfo().PlaybackStatus is
GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing)
this._currentStatus.Timestamps = new Timestamps()
{
End = DateTime.UtcNow.Add(timelineProperties.EndTime - timelineProperties.Position)
};
else
this._currentStatus.Timestamps = new Timestamps();
this._discordRpcClient.SetPresence(this._currentStatus); this._discordRpcClient.SetPresence(this._currentStatus);
} }

View File

@ -13,7 +13,7 @@ if(loglevelIndex > -1)
int discordKeyIndex = Array.IndexOf(args, "-d"); int discordKeyIndex = Array.IndexOf(args, "-d");
string discordKey; string discordKey;
if (loglevelIndex > -1) if (discordKeyIndex > -1)
if(discordKeyIndex + 1 < args.Length) if(discordKeyIndex + 1 < args.Length)
discordKey = args[discordKeyIndex + 1]; discordKey = args[discordKeyIndex + 1];
else else
@ -22,7 +22,7 @@ else
throw new ArgumentNullException(nameof(discordKey)); throw new ArgumentNullException(nameof(discordKey));
int imageKeyIndex = Array.IndexOf(args, "-i"); int imageKeyIndex = Array.IndexOf(args, "-i");
string? imageKey = null; string imageKey = "cat";
if(imageKeyIndex > -1) if(imageKeyIndex > -1)
if (imageKeyIndex + 1 < args.Length) if (imageKeyIndex + 1 < args.Length)
imageKey = args[imageKeyIndex + 1]; imageKey = args[imageKeyIndex + 1];

View File

@ -1,2 +1,11 @@
# DiscordMediaRichPresence # DiscordMediaRichPresence
## Usage
`DiscordMediaRP.exe -d <Your AppliocationKey> [-l Debug]`
Your Application should have 5 Assets:
- cat
- play
- pause
- stop
- music