Using VideoDrawing with MediaPlayer can result in a black frame when going from pause to play amidst UI updates
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
### Description
MS docs is [here](https://docs.microsoft.com/en-us/dotnet/desktop/wpf/graphics-multimedia/how-to-play-media-using-a-videodrawing?view=netframeworkdesktop-4.8) for how to play media using a VideoDrawing. However, I've discovered some issues that can result in a black frame when used in a WPF app. (.NET Framework 4.7.2) The goal is to play + show the video at the same time without a black frame being seen.
If there is some kind of UI update in the same region as the video drawing occurring at the same time transitioning from pause to play, a black frame can occur in that region. A black frame can also occur when the view becomes visible for the first time and trying to play, even when the video is already opened + loaded + paused with [ScrubbingEnabled](https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.mediaelement.scrubbingenabled?view=netframework-4.7.2) set to true. The video itself does not have a black frame.
### Reproduction Steps
Here is a [sample project](https://github.com/ChristopherRoybal-Faithlife/Demo-WMP-BlackFrame).
After pulling, be sure to update the [json file](https://github.com/ChristopherRoybal-Faithlife/Demo-WMP-BlackFrame/blob/master/Demo-WMP-BlackFrame/Config/VideoPlayerSettings.json) to point to an mp4 file on disk.
- Build and run the project
- Switch to `Red rect no delay`
- Video will open, scrub, and pause. A semi-transparent red rectangle will be placed over the middle.
- Then it will hide the red rectangle and play the video at the same time. A black strip will appear where the semi-transparent red rectangle was before.
Here's a [screen recording](https://drive.google.com/file/d/1--JaozLOyDSd22luQGuIvW6W2tf5ue64/view?usp=sharing) of that happening.
### Expected behavior
If a video is already loaded and scrubbed, then it shouldn't display a black region when the `VideoDrawing` becomes visible and plays.
Becoming visible can mean the video player is shown in the UI for the first time. An alternative method is the player is behind another view that is then hidden to "show" the video player.
### Actual behavior
A black region appears when the video player plays and is "shown" by hiding the semi-transparent red rectangle covering it.
### Regression?
_No response_
### Known Workarounds
_No response_
### Impact
Used in presentation software that tries to transition between a slide and the video player with a fading transition. A black frame/flash can sometimes be seen during the transition.
### Configuration
netframework 4.7.2, x64, Windows 10
### Other information
Asked copilot about it and here's what it said:
The black frame appearing in a small region of the VideoDrawing might be due to the way WPF handles rendering updates. When a UI element (like your red rectangle) is removed or changed, WPF redraws the area of the screen that was affected. This process, known as "invalidating" the layout, happens asynchronously and might not coincide exactly with the video frame updates.
When the red rectangle is removed, the area underneath it is invalidated and needs to be redrawn. If the video player doesn't have a frame ready at that exact moment (which can happen if the video is paused), it will display a black frame in that area.
Unfortunately, there's no built-in way to synchronize the layout invalidation with the video frame updates in WPF. This is a limitation of the MediaElement and VideoDrawing classes.
Contributor guide
Assessment
This issue has not been assessed yet.