Skip to content

Pause MediaPlayerElement when its Window closes - #11670

Open
Hemant Kumar (Hemantxk) wants to merge 7 commits into
mainfrom
user/Hemantxk/mpe-pause-play-experiment
Open

Pause MediaPlayerElement when its Window closes#11670
Hemant Kumar (Hemantxk) wants to merge 7 commits into
mainfrom
user/Hemantxk/mpe-pause-play-experiment

Conversation

@Hemantxk

@Hemantxk Hemant Kumar (Hemantxk) commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

Fixes

Related work item: ADO Bug 40049143

PR Type

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no API changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

Description

Current Behavior

When a Window containing a playing MediaPlayerElement is closed, its visual tree is torn down. Until the GC fully collects the closed Window and its content, the underlying MediaPlayer remains alive and can keep playing. The video surface disappears with the Window, but its audio continues in the background.

New Behavior

During a live-tree leave, MediaPlayerElement checks its ContentRoot before the element is detached. If the ContentRoot is shutting down and the player is playing or buffering, the player is paused.

The shutdown check is made regardless of bVisualTreeBeingReset. Window.Close() marks the ContentRoot as PreparingToClose before clearing the Window content, while the resulting leave arrives with bVisualTreeBeingReset == false.

Playback state is read from MediaPlayer_GetCurrentPlaybackSession(), so an active media-break session is considered instead of always reading the main playback session.

Ordinary removal, navigation, recycling, and reparenting do not put the ContentRoot into a shutdown state, so they do not pause playback. No resume-on-re-entry state is needed for this terminal path.

Customer Impact

Closing a playback Window no longer leaves invisible media continuing in the background while the closed Window is waiting to be fully collected by the GC.

There is no public API change.

Regression Potential

The change is limited to live-tree leave during ContentRoot shutdown. It pauses only the active playback session when it is playing or buffering and does not change transport state for ordinary live-tree removal.

  • Low risk — isolated change, limited scope
  • Medium risk — touches shared components or public APIs
  • High risk — architectural or breaking API change

How Has This Been Tested?

  • Manual sample validation confirmed that audio pauses when the playback Window closes while the closed Window is still waiting to be fully collected by the GC.

  • PausesMediaPlayerWhenWindowCloses previously passed in the focused WPF TAEF run. The final revision strengthens it by enabling looping and resetting the paused event immediately before close.

  • I have performed a self-review of my own code

  • I have added tests to cover my changes

  • Existing tests pass locally

Pause a playing MediaPlayer when its element leaves the live tree and resume only when that element initiated the pause. Cover remove/re-entry and retained Window-close lifecycles.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: a3043399-4002-4d60-b928-b71920a36bf0
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@microsoft-github-policy-service microsoft-github-policy-service Bot added the needs-triage Issue needs to be triaged by the area owners label Aug 27, 2026
Pause only when the element leaves because its ContentRoot is shutting down. Remove resume state and retain focused Window-close coverage.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: a3043399-4002-4d60-b928-b71920a36bf0
Keep a source token after the final event macro expansion without retaining the temporary undef directives.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: a3043399-4002-4d60-b928-b71920a36bf0
Pause playing media on live-tree leave and resume when the same element re-enters after initiating that pause. Restore both focused lifecycle tests.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: a3043399-4002-4d60-b928-b71920a36bf0
@Hemantxk
Hemant Kumar (Hemantxk) marked this pull request as ready for review August 27, 2026 13:22
@Hemantxk
Hemant Kumar (Hemantxk) requested a review from a team as a code owner August 27, 2026 13:22
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@Hemantxk

Copy link
Copy Markdown
Collaborator Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
1 pipeline(s) were filtered out due to trigger conditions.

Preserve the original trailing blank line after the final event macro expansion.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: a3043399-4002-4d60-b928-b71920a36bf0
@HariniMalothu17

Copy link
Copy Markdown

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
1 pipeline(s) were filtered out due to trigger conditions.

Comment thread dxaml/xcp/dxaml/lib/MediaPlayerElement_partial.cpp Outdated
Comment thread dxaml/xcp/dxaml/lib/MediaPlayerElement_partial.cpp Outdated
Comment thread dxaml/xcp/dxaml/lib/MediaPlayerElement_partial.cpp Outdated
Comment thread dxaml/xcp/dxaml/lib/MediaPlayerElement_partial.cpp

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is this gated? It changes playback behaviour for existing apps with no velocity or RuntimeEnabledFeature check - ComboBox_Partial.cpp:942 and Slider_Partial.cpp:147 are the pattern in this folder.

Comment thread dxaml/xcp/dxaml/lib/MediaPlayerElement_partial.cpp Outdated
Comment thread dxaml/xcp/dxaml/lib/MediaPlayerElement_partial.cpp Outdated
Comment thread dxaml/xcp/dxaml/lib/MediaPlayerElement_partial.cpp Outdated
Comment thread dxaml/xcp/dxaml/lib/MediaPlayerElement_partial.cpp Outdated
Comment thread dxaml/xcp/dxaml/lib/MediaPlayerElement_partial.cpp Outdated
Comment thread dxaml/xcp/dxaml/lib/MediaPlayerElement_partial.cpp
Comment thread dxaml/xcp/dxaml/lib/MediaPlayerElement_partial.cpp Outdated
Check the ContentRoot lifetime on every live leave, pause only during terminal shutdown, and remove the re-entry resume state. Keep only the Window-close test and make it resistant to stale playback events.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: a3043399-4002-4d60-b928-b71920a36bf0
@Hemantxk Hemant Kumar (Hemantxk) changed the title Pause MediaPlayerElement when leaving the live tree Pause MediaPlayerElement when its Window closes Sep 1, 2026
Query the current ad-break session when checking playback state before pausing during terminal shutdown.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: a3043399-4002-4d60-b928-b71920a36bf0
@Hemantxk

Copy link
Copy Markdown
Collaborator Author

Is this gated? It changes playback behaviour for existing apps with no velocity or RuntimeEnabledFeature check - ComboBox_Partial.cpp:942 and Slider_Partial.cpp:147 are the pattern in this folder.

We don't gate changes in main branch, and RuntimeEnabledFeature is an historic artifact and not recommended for new changes.

@Hemantxk

Copy link
Copy Markdown
Collaborator Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
1 pipeline(s) were filtered out due to trigger conditions.

@iamAbhi-916

Copy link
Copy Markdown
Contributor

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-triage Issue needs to be triaged by the area owners

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants