11using System ;
22using System . Collections . Generic ;
3+ using System . Linq ;
34using System . Threading . Tasks ;
45using Windows . Media . Control ;
56using WindowsMediaController ;
@@ -75,7 +76,11 @@ private void MediaManager_OnSessionOpened(MediaSession mediaSession)
7576
7677 private void MediaManager_OnAnySessionClosed ( MediaSession mediaSession )
7778 {
78- AlbumArtSessions . Remove ( mediaSession ) ;
79+ var artSessionRemoved = AlbumArtSessions . Remove ( mediaSession ) ;
80+ if ( artSessionRemoved )
81+ {
82+ NotifyNextMediaSession ( ) ;
83+ }
7984 MediaSessions . Remove ( mediaSession ) ;
8085 _mediaManager . ForceUpdate ( ) ;
8186 }
@@ -93,17 +98,31 @@ private void MediaManager_OnAnyMediaPropertyChanged(MediaSession mediaSession,
9398 if ( mediaProperties . Thumbnail is null )
9499 {
95100 AlbumArtSessions . Remove ( mediaSession ) ;
96- ArtStateChanged ? . Invoke ( this , new ArtStateChangedEventArgs ( mediaSession , null ) ) ;
101+ NotifyNextMediaSession ( ) ;
97102 return ;
98103 }
99104
100105 AlbumArtSessions . Add ( mediaSession ) ;
101- ArtStateChanged ? . Invoke ( this , new ArtStateChangedEventArgs ( mediaSession , mediaProperties . Thumbnail ) ) ;
106+ ArtStateChanged ? . Invoke ( this , new ArtStateChangedEventArgs ( mediaProperties . Thumbnail ) ) ;
102107 }
103108 catch
104109 {
105110 AlbumArtSessions . Remove ( mediaSession ) ;
106- ArtStateChanged ? . Invoke ( this , new ArtStateChangedEventArgs ( mediaSession , null ) ) ;
111+ NotifyNextMediaSession ( ) ;
112+ }
113+ }
114+
115+ private void NotifyNextMediaSession ( )
116+ {
117+ var nextArtSession = AlbumArtSessions . LastOrDefault ( ) ;
118+ if ( nextArtSession != null )
119+ {
120+ var mediaProperties = nextArtSession . ControlSession . TryGetMediaPropertiesAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
121+ ArtStateChanged ? . Invoke ( this , new ArtStateChangedEventArgs ( mediaProperties . Thumbnail ) ) ;
122+ }
123+ else
124+ {
125+ ArtStateChanged ? . Invoke ( this , new ArtStateChangedEventArgs ( null ) ) ;
107126 }
108127 }
109128
0 commit comments