From 5d332fdfc604f098dfa9671a36c218de9c0bfa18 Mon Sep 17 00:00:00 2001 From: Zhe Fang Date: Sun, 4 Jan 2026 16:02:11 -0500 Subject: [PATCH] fix: media sessions record issue --- .../Package.appxmanifest | 2 +- .../BetterLyrics.WinUI3/App.xaml.cs | 1 + .../Controls/NowPlayingBar.xaml | 13 +++--- .../Controls/NowPlayingBar.xaml.cs | 40 +------------------ .../Services/GSMTCService/GSMTCService.cs | 38 +++++++++--------- 5 files changed, 28 insertions(+), 66 deletions(-) diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3 (Package)/Package.appxmanifest b/BetterLyrics.WinUI3/BetterLyrics.WinUI3 (Package)/Package.appxmanifest index f1d9f27..c4b5f7b 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3 (Package)/Package.appxmanifest +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3 (Package)/Package.appxmanifest @@ -12,7 +12,7 @@ + Version="1.2.241.0" /> diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/App.xaml.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/App.xaml.cs index 93c5895..e45436a 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/App.xaml.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/App.xaml.cs @@ -260,6 +260,7 @@ namespace BetterLyrics.WinUI3 { Log.Logger = new LoggerConfiguration() .MinimumLevel.Is(Serilog.Events.LogEventLevel.Verbose) + .MinimumLevel.Override("Microsoft.EntityFrameworkCore", Serilog.Events.LogEventLevel.Error) .WriteTo.File(PathHelper.LogFilePattern, rollingInterval: RollingInterval.Day) .CreateLogger(); diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/NowPlayingBar.xaml b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/NowPlayingBar.xaml index 9d7c737..97761c0 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/NowPlayingBar.xaml +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/NowPlayingBar.xaml @@ -60,16 +60,16 @@ + Height="36" + Source="{x:Bind ViewModel.GSMTCService.AlbumArtBitmapImage, Mode=OneWay}" /> - + + Foreground="{ThemeResource TextFillColorSecondaryBrush}" + Text="{x:Bind ViewModel.GSMTCService.CurrentSongInfo.DisplayArtists, Mode=OneWay}" /> @@ -423,7 +423,8 @@ Maximum="{x:Bind ViewModel.GSMTCService.CurrentSongInfo.DurationMs, Mode=OneWay, Converter={StaticResource MillisecondsToSecondsConverter}}" Minimum="0" Style="{StaticResource GhostSliderStyle}" - ThumbToolTipValueConverter="{StaticResource SecondsToFormattedTimeConverter}" /> + ThumbToolTipValueConverter="{StaticResource SecondsToFormattedTimeConverter}" + Value="{x:Bind ViewModel.GSMTCService.CurrentPosition.TotalSeconds, Mode=OneWay}" /> >, - IRecipient>, - IRecipient> +public sealed partial class NowPlayingBar : UserControl { public NowPlayingBarViewModel ViewModel => (NowPlayingBarViewModel)DataContext; @@ -110,8 +107,6 @@ public sealed partial class NowPlayingBar : UserControl, { InitializeComponent(); DataContext = Ioc.Default.GetRequiredService(); - - WeakReferenceMessenger.Default.RegisterAll(this); } private static void OnDependencyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) @@ -311,37 +306,4 @@ public sealed partial class NowPlayingBar : UserControl, { PlaybackOrder = PlaybackOrder.GetNext(); } - - public void Receive(PropertyChangedMessage message) - { - if (message.Sender is IGSMTCService) - { - if (message.PropertyName == nameof(IGSMTCService.CurrentSongInfo)) - { - TitleTextBlock.Text = message.NewValue.Title; - ArtistsTextBlock.Text = message.NewValue.DisplayArtists; - } - } - } - public void Receive(PropertyChangedMessage message) - { - if (message.Sender is IGSMTCService) - { - if (message.PropertyName == nameof(IGSMTCService.AlbumArtBitmapImage)) - { - AlbumArtImageSwitcher.Source = message.NewValue; - } - } - } - - public void Receive(PropertyChangedMessage message) - { - if (message.Sender is IGSMTCService) - { - if (message.PropertyName == nameof(IGSMTCService.CurrentPosition)) - { - TimelineSlider.Value = message.NewValue.TotalSeconds; - } - } - } } diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/GSMTCService/GSMTCService.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/GSMTCService/GSMTCService.cs index 2a44503..d663232 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/GSMTCService/GSMTCService.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/GSMTCService/GSMTCService.cs @@ -223,6 +223,8 @@ namespace BetterLyrics.WinUI3.Services.GSMTCService { _mediaManager.Start(); + _mediaManager.CurrentMediaSessions.ToList().ForEach(x => RecordMediaSession(x.Value.Id)); + _mediaManager.OnAnySessionOpened += MediaManager_OnAnySessionOpened; _mediaManager.OnAnySessionClosed += MediaManager_OnAnySessionClosed; _mediaManager.OnFocusedSessionChanged += MediaManager_OnFocusedSessionChanged; @@ -230,7 +232,6 @@ namespace BetterLyrics.WinUI3.Services.GSMTCService _mediaManager.OnAnyPlaybackStateChanged += MediaManager_OnAnyPlaybackStateChanged; _mediaManager.OnAnyTimelinePropertyChanged += MediaManager_OnAnyTimelinePropertyChanged; - _mediaManager.CurrentMediaSessions.ToList().ForEach(x => RecordMediaSourceProviderInfo(x.Value)); OnDesiredSessionChanged(true); } @@ -403,8 +404,22 @@ namespace BetterLyrics.WinUI3.Services.GSMTCService { if (mediaSession == null) return; - RecordMediaSourceProviderInfo(mediaSession); - OnDesiredSessionChanged(); + var id = mediaSession.Id; + + _dispatcherQueue.TryEnqueue(() => + { + RecordMediaSession(id); + OnDesiredSessionChanged(); + }); + } + + private void RecordMediaSession(string id) + { + var found = _settingsService.AppSettings.MediaSourceProvidersInfo.FirstOrDefault(x => x.Provider == id); + if (found == null) + { + _settingsService.AppSettings.MediaSourceProvidersInfo.Add(new MediaSourceProviderInfo(id, _settingsService.AppSettings.GeneralSettings.ListenOnNewPlaybackSource)); + } } private MediaManager.MediaSession? GetCurrentDesiredSession() @@ -425,23 +440,6 @@ namespace BetterLyrics.WinUI3.Services.GSMTCService return null; } - private void RecordMediaSourceProviderInfo(MediaManager.MediaSession mediaSession) - { - if (mediaSession == null) return; - - var id = mediaSession?.Id; - if (string.IsNullOrEmpty(id)) return; - - _dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () => - { - var found = _settingsService.AppSettings.MediaSourceProvidersInfo.FirstOrDefault(x => x.Provider == id); - if (found == null) - { - _settingsService.AppSettings.MediaSourceProvidersInfo.Add(new MediaSourceProviderInfo(id, _settingsService.AppSettings.GeneralSettings.ListenOnNewPlaybackSource)); - } - }); - } - private void SendNullMessages() { _dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () =>