@@ -213,14 +213,14 @@ private void InitMediaManager()
213213 _mediaManager . CurrentMediaSessions . ToList ( ) . ForEach ( x => RecordMediaSourceProviderInfo ( x . Value ) ) ;
214214 }
215215
216- private void MediaManager_OnFocusedSessionChanged ( MediaManager . MediaSession ? mediaSession )
216+ private async void MediaManager_OnFocusedSessionChanged ( MediaManager . MediaSession ? mediaSession )
217217 {
218218 if ( ! _mediaManager . IsStarted ) return ;
219219
220- SendFocusedMessagesAsync ( ) ;
220+ await SendFocusedMessagesAsync ( ) ;
221221 }
222222
223- private void MediaManager_OnAnyTimelinePropertyChanged ( MediaManager . MediaSession mediaSession , GlobalSystemMediaTransportControlsSessionTimelineProperties timelineProperties )
223+ private void MediaManager_OnAnyTimelinePropertyChanged ( MediaManager . MediaSession ? mediaSession , GlobalSystemMediaTransportControlsSessionTimelineProperties ? timelineProperties )
224224 {
225225 if ( ! _mediaManager . IsStarted ) return ;
226226 if ( mediaSession == null ) return ;
@@ -241,16 +241,16 @@ private void MediaManager_OnAnyTimelinePropertyChanged(MediaManager.MediaSession
241241 {
242242 if ( IsMediaSourceTimelineSyncEnabled ( mediaSession . Id ) )
243243 {
244- _cachedPosition = timelineProperties . Position ;
244+ _cachedPosition = timelineProperties ? . Position ?? TimeSpan . Zero ;
245245 _dispatcherQueue . TryEnqueue ( DispatcherQueuePriority . Low , ( ) =>
246246 {
247- TimelineChanged ? . Invoke ( this , new TimelineChangedEventArgs ( _cachedPosition , timelineProperties . EndTime ) ) ;
247+ TimelineChanged ? . Invoke ( this , new TimelineChangedEventArgs ( _cachedPosition , timelineProperties ? . EndTime ?? TimeSpan . Zero ) ) ;
248248 } ) ;
249249 }
250250 }
251251 }
252252
253- private void MediaManager_OnAnyPlaybackStateChanged ( MediaManager . MediaSession mediaSession , GlobalSystemMediaTransportControlsSessionPlaybackInfo playbackInfo )
253+ private void MediaManager_OnAnyPlaybackStateChanged ( MediaManager . MediaSession ? mediaSession , GlobalSystemMediaTransportControlsSessionPlaybackInfo ? playbackInfo )
254254 {
255255 _dispatcherQueue . TryEnqueue ( DispatcherQueuePriority . Low , ( ) =>
256256 {
@@ -268,7 +268,7 @@ private void MediaManager_OnAnyPlaybackStateChanged(MediaManager.MediaSession me
268268 }
269269 else
270270 {
271- _cachedIsPlaying = playbackInfo . PlaybackStatus switch
271+ _cachedIsPlaying = playbackInfo ? . PlaybackStatus switch
272272 {
273273 GlobalSystemMediaTransportControlsSessionPlaybackStatus . Playing => true ,
274274 _ => false ,
@@ -279,26 +279,28 @@ private void MediaManager_OnAnyPlaybackStateChanged(MediaManager.MediaSession me
279279 } ) ;
280280 }
281281
282- private void MediaManager_OnAnyMediaPropertyChanged ( MediaManager . MediaSession mediaSession , GlobalSystemMediaTransportControlsSessionMediaProperties mediaProperties )
282+ private void MediaManager_OnAnyMediaPropertyChanged ( MediaManager . MediaSession ? mediaSession , GlobalSystemMediaTransportControlsSessionMediaProperties ? mediaProperties )
283283 {
284284 _dispatcherQueue . TryEnqueue ( DispatcherQueuePriority . Low , async ( ) =>
285285 {
286286 if ( ! _mediaManager . IsStarted ) return ;
287- if ( mediaSession == null ) return ;
287+ if ( mediaSession == null )
288+ {
289+ _cachedSongInfo = SongInfoExtensions . Placeholder ;
290+ }
288291
289- string sessionId = mediaSession . Id ;
292+ string ? sessionId = mediaSession ? . Id ;
290293
291294 var desiredSession = GetCurrentSession ( ) ;
292295
293- //RecordMediaSourceProviderInfo(mediaSession);
294296 if ( mediaSession != desiredSession ) return ;
295297
296- if ( ! IsMediaSourceEnabled ( sessionId ) )
298+ if ( sessionId != null && ! IsMediaSourceEnabled ( sessionId ) )
297299 {
298- _cachedSongInfo = null ;
300+ _cachedSongInfo = SongInfoExtensions . Placeholder ;
299301
300302 _logger . LogInformation ( "Media properties changed: Title: {Title}, Artist: {Artist}, Album: {Album}" ,
301- mediaProperties . Title , mediaProperties . Artist , mediaProperties . AlbumTitle ) ;
303+ mediaProperties ? . Title , mediaProperties ? . Artist , mediaProperties ? . AlbumTitle ) ;
302304
303305 if ( sessionId == Constants . PlayerID . LXMusic )
304306 {
@@ -315,33 +317,33 @@ private void MediaManager_OnAnyMediaPropertyChanged(MediaManager.MediaSession me
315317 currentMediaSourceProviderInfo ? . PositionOffset = 0 ;
316318 }
317319
318- string fixedArtist = mediaProperties . Artist ;
319- string fixedAlbum = mediaProperties . AlbumTitle ;
320+ string fixedArtist = mediaProperties ? . Artist ?? "N/A" ;
321+ string fixedAlbum = mediaProperties ? . AlbumTitle ?? "N/A" ;
320322 string ? songId = null ;
321323
322324 if ( sessionId == Constants . PlayerID . AppleMusic || sessionId == Constants . PlayerID . AppleMusicAlternative )
323325 {
324- fixedArtist = mediaProperties . Artist . Split ( " — " ) . FirstOrDefault ( ) ?? mediaProperties . Artist ;
325- fixedAlbum = mediaProperties . Artist . Split ( " — " ) . LastOrDefault ( ) ?? mediaProperties . AlbumTitle ;
326+ fixedArtist = mediaProperties ? . Artist . Split ( " — " ) . FirstOrDefault ( ) ?? ( mediaProperties ? . Artist ?? "N/A" ) ;
327+ fixedAlbum = mediaProperties ? . Artist . Split ( " — " ) . LastOrDefault ( ) ?? ( mediaProperties ? . AlbumTitle ?? "N/A" ) ;
326328 }
327- else if ( PlayerIdMatcher . IsNeteaseFamily ( sessionId ) )
329+ else if ( PlayerIdMatcher . IsNeteaseFamily ( sessionId ?? "" ) )
328330 {
329- songId = mediaProperties . Genres . FirstOrDefault ( ) ? . Replace ( "NCM-" , "" ) ;
331+ songId = mediaProperties ? . Genres . FirstOrDefault ( ) ? . Replace ( "NCM-" , "" ) ;
330332 }
331333
332334 _cachedSongInfo = new SongInfo
333335 {
334- Title = mediaProperties . Title ,
336+ Title = mediaProperties ? . Title ?? "N/A" ,
335337 Artist = fixedArtist ,
336338 Album = fixedAlbum ,
337- DurationMs = mediaSession . ControlSession . GetTimelineProperties ( ) . EndTime . TotalMilliseconds ,
339+ DurationMs = mediaSession ? . ControlSession ? . GetTimelineProperties ( ) . EndTime . TotalMilliseconds ,
338340 PlayerId = sessionId ,
339341 SongId = songId
340342 } ;
341- _cachedSongInfo . Duration = ( int ) ( _cachedSongInfo . DurationMs / 1000f ) ;
343+ _cachedSongInfo . Duration = ( int ) ( ( _cachedSongInfo . DurationMs ?? 0 ) / 1000f ) ;
342344
343345 _logger . LogInformation ( "Media properties changed: Title: {Title}, Artist: {Artist}, Album: {Album}" ,
344- mediaProperties . Title , mediaProperties . Artist , mediaProperties . AlbumTitle ) ;
346+ mediaProperties ? . Title , mediaProperties ? . Artist , mediaProperties ? . AlbumTitle ) ;
345347
346348 if ( sessionId == Constants . PlayerID . LXMusic )
347349 {
@@ -356,7 +358,7 @@ private void MediaManager_OnAnyMediaPropertyChanged(MediaManager.MediaSession me
356358 {
357359 _SMTCAlbumArtBuffer = _lxMusicAlbumArtBytes . AsBuffer ( ) ;
358360 }
359- else if ( mediaProperties . Thumbnail is IRandomAccessStreamReference streamReference )
361+ else if ( mediaProperties ? . Thumbnail is IRandomAccessStreamReference streamReference )
360362 {
361363 _SMTCAlbumArtBuffer = await ImageHelper . ToBufferAsync ( streamReference ) ;
362364 }
@@ -438,7 +440,7 @@ private void SendNullMessages()
438440 {
439441 _dispatcherQueue . TryEnqueue ( DispatcherQueuePriority . Low , ( ) =>
440442 {
441- _cachedSongInfo = null ;
443+ _cachedSongInfo = SongInfoExtensions . Placeholder ;
442444 _cachedIsPlaying = false ;
443445 SongInfoChanged ? . Invoke ( this , new SongInfoChangedEventArgs ( _cachedSongInfo ) ) ;
444446 IsPlayingChanged ? . Invoke ( this , new IsPlayingChangedEventArgs ( _cachedIsPlaying ) ) ;
@@ -448,14 +450,21 @@ private void SendNullMessages()
448450
449451 private async Task SendFocusedMessagesAsync ( )
450452 {
453+ GlobalSystemMediaTransportControlsSessionMediaProperties ? mediaProps = null ;
454+
451455 var desiredSession = GetCurrentSession ( ) ;
452- if ( desiredSession == null || desiredSession . ControlSession == null ) return ;
456+ //if (desiredSession == null || desiredSession.ControlSession == null) return;
457+
458+ try
459+ {
460+ mediaProps = await desiredSession ? . ControlSession ? . TryGetMediaPropertiesAsync ( ) ;
461+ }
462+ catch ( Exception ) { }
463+ //if (desiredSession == null || desiredSession.ControlSession == null) return;
453464
454- var mediaProps = await desiredSession . ControlSession . TryGetMediaPropertiesAsync ( ) ;
455- if ( desiredSession == null || desiredSession . ControlSession == null ) return ;
456- MediaManager_OnAnyTimelinePropertyChanged ( desiredSession , desiredSession . ControlSession . GetTimelineProperties ( ) ) ;
465+ MediaManager_OnAnyTimelinePropertyChanged ( desiredSession , desiredSession ? . ControlSession ? . GetTimelineProperties ( ) ) ;
457466 MediaManager_OnAnyMediaPropertyChanged ( desiredSession , mediaProps ) ;
458- MediaManager_OnAnyPlaybackStateChanged ( desiredSession , desiredSession . ControlSession . GetPlaybackInfo ( ) ) ;
467+ MediaManager_OnAnyPlaybackStateChanged ( desiredSession , desiredSession ? . ControlSession ? . GetPlaybackInfo ( ) ) ;
459468 }
460469
461470 private void StartSSE ( )
0 commit comments