55using Artemis . MediaInfo . DataModels ;
66using WindowsMediaController ;
77using System ;
8- using System . Text ;
98using System . Threading . Tasks ;
10- using System . Windows ;
119using Windows . Media ;
1210using Windows . Storage . Streams ;
1311using SkiaSharp ;
1412using Artemis . Core . Services ;
1513using Windows . Media . Control ;
14+ using static WindowsMediaController . MediaManager ;
1615
1716namespace Artemis . MediaInfo
1817{
@@ -24,8 +23,8 @@ public class MediaInfoModule : Module<MediaInfoDataModel>
2423
2524 public override List < IModuleActivationRequirement > ActivationRequirements { get ; } = new ( ) ;
2625
27- private readonly HashSet < MediaManager . MediaSession > _mediaSessions = new ( new MediaSessionComparer ( ) ) ;
28- private readonly HashSet < MediaManager . MediaSession > _albumArtSessions = new ( new MediaSessionComparer ( ) ) ;
26+ private readonly HashSet < MediaSession > _mediaSessions = new ( new MediaSessionComparer ( ) ) ;
27+ private readonly HashSet < MediaSession > _albumArtSessions = new ( new MediaSessionComparer ( ) ) ;
2928
3029 public MediaInfoModule ( IColorQuantizerService colorQuantizerService )
3130 {
@@ -46,9 +45,17 @@ public override void Enable()
4645 if ( ! DataModel . HasMedia ) return ;
4746
4847 _mediaSessions . Clear ( ) ;
48+ _albumArtSessions . Clear ( ) ;
4949 foreach ( var ( _, mediaSession ) in _mediaManager . CurrentMediaSessions )
5050 {
5151 _mediaSessions . Add ( mediaSession ) ;
52+ mediaSession . ControlSession . TryGetMediaPropertiesAsync ( ) . AsTask ( ) . ContinueWith ( task =>
53+ {
54+ if ( task . Result . Thumbnail != null )
55+ {
56+ _albumArtSessions . Add ( mediaSession ) ;
57+ }
58+ } ) ;
5259 }
5360
5461 DataModel . MediaSessions = _mediaSessions ; //debug
@@ -75,21 +82,23 @@ public override void Update(double deltaTime) {}
7582 public override void ModuleActivated ( bool isOverride ) { }
7683 public override void ModuleDeactivated ( bool isOverride ) { }
7784
78- private void MediaManager_OnSessionOpened ( MediaManager . MediaSession mediaSession )
85+ private void MediaManager_OnSessionOpened ( MediaSession mediaSession )
7986 {
8087 DataModel . HasMedia = true ;
8188 _mediaSessions . Add ( mediaSession ) ;
89+
90+ UpdateButtons ( ) ;
8291 }
8392
84- private void MediaManager_OnAnySessionClosed ( MediaManager . MediaSession mediaSession )
93+ private void MediaManager_OnAnySessionClosed ( MediaSession mediaSession )
8594 {
8695 _mediaSessions . Remove ( mediaSession ) ;
8796 _albumArtSessions . Remove ( mediaSession ) ;
8897
8998 UpdateButtons ( ) ;
9099 }
91100
92- private async void MediaManager_OnAnyMediaPropertyChanged ( MediaManager . MediaSession mediaSession ,
101+ private async void MediaManager_OnAnyMediaPropertyChanged ( MediaSession mediaSession ,
93102 GlobalSystemMediaTransportControlsSessionMediaProperties mediaProperties )
94103 {
95104 DataModel . MediaChanged . Trigger ( new MediaChangedEventArgs
@@ -110,16 +119,7 @@ private async void MediaManager_OnAnyMediaPropertyChanged(MediaManager.MediaSess
110119 return ;
111120 }
112121
113- var imageStream = await mediaProperties . Thumbnail . OpenReadAsync ( ) ;
114- var fileBytes = new byte [ imageStream . Size ] ;
115-
116- using DataReader reader = new DataReader ( imageStream ) ;
117- await reader . LoadAsync ( ( uint ) imageStream . Size ) ;
118- reader . ReadBytes ( fileBytes ) ;
119-
120- using SKBitmap skbm = SKBitmap . Decode ( fileBytes ) ;
121- SKColor [ ] skClrs = _colorQuantizer . Quantize ( skbm . Pixels , 256 ) ;
122- DataModel . ArtColors = _colorQuantizer . FindAllColorVariations ( skClrs , true ) ;
122+ DataModel . ArtColors = await ReadMediaColors ( mediaProperties ) ;
123123 DataModel . HasArt = true ;
124124 _albumArtSessions . Add ( mediaSession ) ;
125125 }
@@ -131,7 +131,23 @@ private async void MediaManager_OnAnyMediaPropertyChanged(MediaManager.MediaSess
131131 }
132132 }
133133
134- private void MediaManager_OnAnyPlaybackStateChanged ( MediaManager . MediaSession mediaSession ,
134+ private async Task < ColorSwatch > ReadMediaColors ( GlobalSystemMediaTransportControlsSessionMediaProperties mediaProperties )
135+ {
136+ var imageStream = await mediaProperties . Thumbnail . OpenReadAsync ( ) ;
137+ var fileBytes = new byte [ imageStream . Size ] ;
138+
139+ using ( var reader = new DataReader ( imageStream ) )
140+ {
141+ await reader . LoadAsync ( ( uint ) imageStream . Size ) ;
142+ reader . ReadBytes ( fileBytes ) ;
143+ }
144+
145+ using SKBitmap bitmap = SKBitmap . Decode ( fileBytes ) ;
146+ SKColor [ ] skClrs = _colorQuantizer . Quantize ( bitmap . Pixels , 256 ) ;
147+ return _colorQuantizer . FindAllColorVariations ( skClrs , true ) ; ;
148+ }
149+
150+ private void MediaManager_OnAnyPlaybackStateChanged ( MediaSession mediaSession ,
135151 GlobalSystemMediaTransportControlsSessionPlaybackInfo playbackInfo )
136152 {
137153 UpdateButtons ( ) ;
@@ -151,14 +167,14 @@ private void UpdateButtons()
151167 DataModel . HasArt = _albumArtSessions . Count > 0 ;
152168 }
153169
154- private class MediaSessionComparer : IEqualityComparer < MediaManager . MediaSession >
170+ private class MediaSessionComparer : IEqualityComparer < MediaSession >
155171 {
156- public bool Equals ( MediaManager . MediaSession x , MediaManager . MediaSession y )
172+ public bool Equals ( MediaSession x , MediaSession y )
157173 {
158174 return x ? . Id == y ? . Id ;
159175 }
160176
161- public int GetHashCode ( MediaManager . MediaSession obj )
177+ public int GetHashCode ( MediaSession obj )
162178 {
163179 return obj . Id . GetHashCode ( ) ;
164180 }
0 commit comments