Skip to content

Commit

Permalink
many of the bugs fixed
Browse files Browse the repository at this point in the history
added MediaChangedEvent
  • Loading branch information
aytac.kayadelen committed Sep 5, 2022
1 parent cb188e2 commit 51c2f6c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Artemis.MediaInfo/Artemis.MediaInfo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ItemGroup>

<ItemGroup>
<Reference Include="Dubya.WindowsMediaController" Version="2.1.0" >
<Reference Include="Dubya.WindowsMediaController" Version="2.1.0">
<HintPath>.\WindowsMediaController.dll</HintPath>
<Private>true</Private>
</Reference>
Expand Down
14 changes: 13 additions & 1 deletion Artemis.MediaInfo/DataModels/MediaInfoDataModel.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -30,5 +31,16 @@ public class MediaInfoDataModel : DataModel
public bool HasArt { get; set; }

public ColorSwatch ArtColors { get; set; }

public DataModelEvent<MediaChangedEventArgs> 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; }
}
}
98 changes: 47 additions & 51 deletions Artemis.MediaInfo/MediaInfoModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,7 +24,8 @@ public class MediaInfoModule : Module<MediaInfoDataModel>

public override List<IModuleActivationRequirement> ActivationRequirements { get; } = new();

private List<MediaManager.MediaSession> _mediaSessions = new();
private readonly HashSet<MediaManager.MediaSession> _mediaSessions = new(new MediaSessionComparer());
private readonly HashSet<MediaManager.MediaSession> _albumArtSessions = new(new MediaSessionComparer());

public MediaInfoModule(IColorQuantizerService colorQuantizerService)
{
Expand All @@ -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<MediaManager.MediaSession>();
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()
Expand All @@ -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;
}

Expand All @@ -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
{
Expand All @@ -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<MediaManager.MediaSession>
{
public bool Equals(MediaManager.MediaSession x, MediaManager.MediaSession y)
{
return x?.Id == y?.Id;
}

public int GetHashCode(MediaManager.MediaSession obj)
{
return obj.Id.GetHashCode();
}
}
}
}
2 changes: 1 addition & 1 deletion Artemis.MediaInfo/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 51c2f6c

Please sign in to comment.