Skip to content

Conversation

@ThanhNguyxn
Copy link

Summary of the Pull Request

Fixes #26755

When previewing video or audio files in Peek and then closing the window, Peek continues to appear in the Windows System Media Transport Controls (SMTC) media player panel. This PR fixes this issue by properly unregistering the MediaPlayer from SMTC when media sources are cleared.

PR Checklist

  • Closes [Peek] Peek.UI persists as a media player after closed #26755
  • Communication: I've discussed this with core contributors already. If work hasn't been discussed, please file an issue with a proposal.
  • Tests: Added/updated and all pass. (No new tests needed - this is a behavioral fix for media player cleanup)
  • Localization: All end-user facing strings can be localized.
  • Dev docs: It's not applicable
  • New binaries: I've not introduced any new dependencies or binary files
  • Changelog: I've not added a one-liner to the CHANGELOG about this

Technical Details

Root Cause

The MediaPlayer in WinUI 3 automatically integrates with Windows System Media Transport Controls (SMTC). When the Peek window is closed:

  1. The video/audio source is set to null
  2. The media player is paused
  3. However, the CommandManager remains registered with SMTC, causing the player to persist in the media controls panel

Solution

According to Microsoft documentation, setting CommandManager.IsEnabled = false properly unregisters the media player from SMTC.

This PR:

  1. FilePreview.xaml.cs:

    • Disables CommandManager.IsEnabled when clearing the video source in OnPreviewerChanging()
    • Re-enables it when a new source is loaded via SourceChanged event handler
  2. AudioControl.xaml.cs:

    • Disables CommandManager.IsEnabled when audio source is set to null
    • Re-enables it when a new audio source is set

Testing

  1. Open Peek and preview a video file → video plays and appears in SMTC
  2. Close Peek window → Peek no longer appears in SMTC panel ✅
  3. Open Peek and preview an audio file → audio plays and appears in SMTC
  4. Close Peek window → Peek no longer appears in SMTC panel ✅
  5. Re-open Peek with another media file → transport controls work normally ✅

Copilot AI review requested due to automatic review settings December 3, 2025 03:36
Copilot finished reviewing on behalf of ThanhNguyxn December 3, 2025 03:39
@github-actions

This comment has been minimized.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an issue where Peek persists in Windows System Media Transport Controls (SMTC) after closing the window. The solution disables MediaPlayer.CommandManager.IsEnabled when clearing media sources and re-enables it when new sources are loaded.

Key Changes:

  • Properly unregister media players from SMTC by disabling CommandManager.IsEnabled when sources are cleared
  • Re-enable CommandManager.IsEnabled when new media sources are loaded to restore transport controls functionality
  • Implement proper event handler lifecycle management for video media player

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/modules/peek/Peek.FilePreviewer/FilePreview.xaml.cs Adds SourceChanged event handler to re-enable CommandManager after video source changes, disables CommandManager in OnPreviewerChanging when clearing video source, and properly unsubscribes event handler in Dispose
src/modules/peek/Peek.FilePreviewer/Controls/AudioControl.xaml.cs Disables CommandManager when audio source is null and re-enables it when a non-null source is set in SourcePropertyChanged callback

Comment on lines 54 to 58
else
{
// Re-enable CommandManager when a new source is set so transport controls work
mediaPlayer.CommandManager.IsEnabled = true;
}
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting CommandManager.IsEnabled = true when Source is not null may occur before the MediaPlayer's source is actually set. The Source property is of type AudioPreviewData, and the actual MediaSource is bound via XAML binding (Source.MediaSource). This means the XAML binding hasn't necessarily evaluated yet when this code executes.

Consider using a SourceChanged event handler on PlayerElement.MediaPlayer (similar to how it's done in FilePreview.xaml.cs for video) to ensure the CommandManager is re-enabled only after the MediaPlayer source is actually set:

public AudioControl()
{
    this.InitializeComponent();
    PlayerElement.MediaPlayer.SourceChanged += AudioMediaPlayer_SourceChanged;
}

private void AudioMediaPlayer_SourceChanged(Windows.Media.Playback.MediaPlayer sender, object args)
{
    if (sender.Source != null)
    {
        sender.CommandManager.IsEnabled = true;
    }
}

Don't forget to unsubscribe from this event if a Dispose method is added.

Copilot uses AI. Check for mistakes.
@ThanhNguyxn
Copy link
Author

@microsoft-github-policy-service agree

@ThanhNguyxn ThanhNguyxn force-pushed the fix/peek-media-player-smtc branch 3 times, most recently from 5c3208a to 3ecd669 Compare December 5, 2025 10:52
Fixes microsoft#26755

When previewing video or audio files in Peek and closing the window,
the app would persist in Windows System Media Transport Controls (SMTC)
media player panel. This happened because the MediaPlayer was only
paused and source cleared, but not properly unregistered from SMTC.

The fix disables MediaPlayer.CommandManager.IsEnabled when clearing
the source to remove the app from SMTC. The CommandManager is re-enabled
when a new media source is loaded to restore transport controls
functionality.
@ThanhNguyxn ThanhNguyxn force-pushed the fix/peek-media-player-smtc branch from 3ecd669 to 788991d Compare December 5, 2025 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Peek] Peek.UI persists as a media player after closed

1 participant