From 51c2f6c4258d9e6b7af12c460882ff53afe4ed5f Mon Sep 17 00:00:00 2001 From: "aytac.kayadelen" Date: Mon, 5 Sep 2022 12:27:30 +0300 Subject: [PATCH] many of the bugs fixed added MediaChangedEvent --- Artemis.MediaInfo/Artemis.MediaInfo.csproj | 2 +- .../DataModels/MediaInfoDataModel.cs | 14 ++- Artemis.MediaInfo/MediaInfoModule.cs | 98 +++++++++---------- Artemis.MediaInfo/plugin.json | 2 +- 4 files changed, 62 insertions(+), 54 deletions(-) diff --git a/Artemis.MediaInfo/Artemis.MediaInfo.csproj b/Artemis.MediaInfo/Artemis.MediaInfo.csproj index ae3cb0b..43d850c 100644 --- a/Artemis.MediaInfo/Artemis.MediaInfo.csproj +++ b/Artemis.MediaInfo/Artemis.MediaInfo.csproj @@ -13,7 +13,7 @@ - + .\WindowsMediaController.dll true diff --git a/Artemis.MediaInfo/DataModels/MediaInfoDataModel.cs b/Artemis.MediaInfo/DataModels/MediaInfoDataModel.cs index 5debcbf..ae1db7d 100644 --- a/Artemis.MediaInfo/DataModels/MediaInfoDataModel.cs +++ b/Artemis.MediaInfo/DataModels/MediaInfoDataModel.cs @@ -1,4 +1,5 @@ -using Windows.Media.Control; +using Windows.Media; +using Windows.Media.Control; using Artemis.Core; using Artemis.Core.Modules; using Artemis.Core.Services; @@ -30,5 +31,16 @@ public class MediaInfoDataModel : DataModel public bool HasArt { get; set; } public ColorSwatch ArtColors { get; set; } + + public DataModelEvent MediaChanged { get; } = new(); + } + + public class MediaChangedEventArgs : DataModelEventArgs + { + public string SessionId { get; set; } + public string Title { get; set; } + public string Artist { get; set; } + public MediaPlaybackType MediaType { get; set; } + public bool HasArt { get; set; } } } diff --git a/Artemis.MediaInfo/MediaInfoModule.cs b/Artemis.MediaInfo/MediaInfoModule.cs index 578ead1..4984981 100644 --- a/Artemis.MediaInfo/MediaInfoModule.cs +++ b/Artemis.MediaInfo/MediaInfoModule.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows; +using Windows.Media; using Windows.Storage.Streams; using SkiaSharp; using Artemis.Core.Services; @@ -23,7 +24,8 @@ public class MediaInfoModule : Module public override List ActivationRequirements { get; } = new(); - private List _mediaSessions = new(); + private readonly HashSet _mediaSessions = new(new MediaSessionComparer()); + private readonly HashSet _albumArtSessions = new(new MediaSessionComparer()); public MediaInfoModule(IColorQuantizerService colorQuantizerService) { @@ -34,27 +36,22 @@ public override void Enable() { _mediaManager = new MediaManager(); _mediaManager.OnAnySessionOpened += MediaManager_OnSessionOpened; - _mediaManager.OnAnyMediaPropertyChanged += MediaManager_OnAnyMediaPropertyChanged; _mediaManager.OnAnyPlaybackStateChanged += MediaManager_OnAnyPlaybackStateChanged; + _mediaManager.OnAnyMediaPropertyChanged += MediaManager_OnAnyMediaPropertyChanged; _mediaManager.OnAnySessionClosed += MediaManager_OnAnySessionClosed; _mediaManager.Start(); - var mediaExist = _mediaManager.CurrentMediaSessions.Count > 0; - DataModel.HasMedia = mediaExist; - - _mediaSessions = new List(); - if (!mediaExist) return; + DataModel.HasMedia = _mediaManager.CurrentMediaSessions.Count > 0; + if (!DataModel.HasMedia) return; + _mediaSessions.Clear(); foreach (var (_, mediaSession) in _mediaManager.CurrentMediaSessions) { - ListenSession(mediaSession); + _mediaSessions.Add(mediaSession); } - DataModel.HasNextMedia = _mediaManager.CurrentMediaSessions.Any(pair => pair.Value.ControlSession.GetPlaybackInfo().Controls.IsNextEnabled); - DataModel.HasPreviousMedia = _mediaManager.CurrentMediaSessions.Any(pair => pair.Value.ControlSession.GetPlaybackInfo().Controls.IsPreviousEnabled); - DataModel.MediaPlaying = _mediaManager.CurrentMediaSessions.Any(pair => pair.Value.ControlSession.GetPlaybackInfo().PlaybackStatus == - GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing); + UpdateButtons(); } public override void Disable() @@ -65,65 +62,49 @@ public override void Disable() } _mediaManager.OnAnySessionOpened -= MediaManager_OnSessionOpened; - _mediaManager.OnAnyMediaPropertyChanged -= MediaManager_OnAnyMediaPropertyChanged; _mediaManager.OnAnyPlaybackStateChanged -= MediaManager_OnAnyPlaybackStateChanged; + _mediaManager.OnAnyMediaPropertyChanged -= MediaManager_OnAnyMediaPropertyChanged; _mediaManager.OnAnySessionClosed -= MediaManager_OnAnySessionClosed; _mediaManager.Dispose(); _mediaManager = null; } - public override void Update(double deltaTime) - { - - } - - public override void ModuleActivated(bool isOverride) - { - - } - - public override void ModuleDeactivated(bool isOverride) - { - - } + public override void Update(double deltaTime) {} + public override void ModuleActivated(bool isOverride) {} + public override void ModuleDeactivated(bool isOverride) {} private void MediaManager_OnSessionOpened(MediaManager.MediaSession mediaSession) { DataModel.HasMedia = true; - ListenSession(mediaSession); - } - - private void ListenSession(MediaManager.MediaSession mediaSession) - { _mediaSessions.Add(mediaSession); } private void MediaManager_OnAnySessionClosed(MediaManager.MediaSession mediaSession) { - mediaSession.OnMediaPropertyChanged -= MediaManager_OnAnyMediaPropertyChanged; - mediaSession.OnPlaybackStateChanged -= MediaManager_OnAnyPlaybackStateChanged; - mediaSession.OnSessionClosed -= MediaManager_OnAnySessionClosed; _mediaSessions.Remove(mediaSession); + _albumArtSessions.Remove(mediaSession); - if (_mediaSessions.Count == 0) - { - DataModel.HasMedia = false; - DataModel.HasNextMedia = false; - DataModel.HasPreviousMedia = false; - } - else - { - UpdateButtons(); - } + UpdateButtons(); } - private async void MediaManager_OnAnyMediaPropertyChanged(MediaManager.MediaSession mediaSession, GlobalSystemMediaTransportControlsSessionMediaProperties mediaProperties) + private async void MediaManager_OnAnyMediaPropertyChanged(MediaManager.MediaSession mediaSession, + GlobalSystemMediaTransportControlsSessionMediaProperties mediaProperties) { + DataModel.MediaChanged.Trigger(new MediaChangedEventArgs + { + SessionId = mediaSession.Id, + Title = mediaProperties.Title, + Artist = mediaProperties.Artist, + MediaType = mediaProperties.PlaybackType ?? MediaPlaybackType.Unknown, + HasArt = mediaProperties.Thumbnail is not null + }); + try { if (mediaProperties.Thumbnail is null) { - DataModel.HasArt = false; + _albumArtSessions.Remove(mediaSession); + DataModel.HasArt = _albumArtSessions.Count > 0; return; } @@ -138,6 +119,7 @@ private async void MediaManager_OnAnyMediaPropertyChanged(MediaManager.MediaSess SKColor[] skClrs = _colorQuantizer.Quantize(skbm.Pixels, 256); DataModel.ArtColors = _colorQuantizer.FindAllColorVariations(skClrs, true); DataModel.HasArt = true; + _albumArtSessions.Add(mediaSession); } catch { @@ -156,11 +138,25 @@ private void UpdateButtons() { DataModel.HasNextMedia = _mediaSessions.Any( value => value.ControlSession.GetPlaybackInfo().Controls.IsNextEnabled); - DataModel.HasPreviousMedia = _mediaManager.CurrentMediaSessions.Any(value => - value.Value.ControlSession.GetPlaybackInfo().Controls.IsPreviousEnabled); - DataModel.MediaPlaying = _mediaManager.CurrentMediaSessions.Any(value => - value.Value.ControlSession.GetPlaybackInfo().PlaybackStatus == + DataModel.HasPreviousMedia = _mediaSessions.Any(value => + value.ControlSession.GetPlaybackInfo().Controls.IsPreviousEnabled); + DataModel.MediaPlaying = _mediaSessions.Any(value => + value.ControlSession.GetPlaybackInfo().PlaybackStatus == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing); + DataModel.HasArt = _albumArtSessions.Count > 0; + } + + private class MediaSessionComparer : IEqualityComparer + { + public bool Equals(MediaManager.MediaSession x, MediaManager.MediaSession y) + { + return x?.Id == y?.Id; + } + + public int GetHashCode(MediaManager.MediaSession obj) + { + return obj.Id.GetHashCode(); + } } } } \ No newline at end of file diff --git a/Artemis.MediaInfo/plugin.json b/Artemis.MediaInfo/plugin.json index e6bbd85..cee4ec8 100644 --- a/Artemis.MediaInfo/plugin.json +++ b/Artemis.MediaInfo/plugin.json @@ -6,6 +6,6 @@ "Main": "Artemis.MediaInfo.dll", "Name": "Media Info", "Repository": "https://github.com/Aytackydln/Artemis.MediaInfo", - "Version": "0.0.3.1", + "Version": "0.0.4.0", "Platforms":"Windows" }