Why does WPF MediaElement not work on secondary monitor?
Asked Answered
A

2

22

My application uses the WPF MediaElement to play video (MOV files). This works well when playing on the Primary monitor but freezes when the window is moved to the secondary monitor.

I have tried the following without success:

  1. Starting the application on the secondary monitor
  2. Swapping the primary & secondary monitors (problem transfers to the new secondary monitor)

When the application window spans both monitors it works correctly but as soon as it is entirely within the secondary monitor the video freezes. Once in this state, moving the application back to the primary monitor doesn't help (and loading a new video doesn't help either).

The monitors are arranged so that the co-ordinates are always positive (both monitors are 1920x1080 and the secondary monitor origin is 1920,0).

Has anyone else seen this problem and/or found a fix?

EDIT

Does anyone use the WPF MediaElement with multiple monitors???

Adagio answered 15/11, 2010 at 22:51 Comment(5)
Could it be that the secondary monitor is not accelerated by the GPU? Is this consistent across many PC's or just on one?Clepsydra
Only tried on one laptop so far. The secondary monitor in my normal configuration is the laptop screen. The problem still occurs if I make the external monitor the secondary monitor. BTW, Windows Media Player works fine on both the primary & secondary with the same movie.Adagio
WPFMediaKit also works on both monitors....Adagio
Found another laptop that doesn't have this problem. Never found a solution for my laptop.Adagio
I can confirm this bug. I'm using the MediaElement to play a video and it always freezes on the window spawns on the secondary monitor.Flowery
E
31

This is still a known issue in .NET Framework 4.0, which MS described as "The issue occurs when a synchronization between WPF and the underlying WMP control have to resynchronize when the display changes occur." It happens to H.264 codec video files.


Here are 3 workarounds.

1 . Use software rendering for the window containing the MediaElement control

private void Window_Loaded(object sender, RoutedEventArgs e)
{
        var hwndSource = PresentationSource.FromVisual(this) as HwndSource;
        if (hwndSource != null)
        {
            var hwndTarget = hwndSource.CompositionTarget;
            if (hwndTarget != null) hwndTarget.RenderMode = RenderMode.SoftwareOnly;
        }
}

However this is not utilizing the GPU and graphics memory and could slow down the video playback.


2. Overlap at least 1 pixel onto the primary display

For example, suppose the primary screen in on the left, and the MediaElement fills the entire window. In the window's constructor, suppose Rect bounds represents the secondary monitor boundary, use

this.Left = bounds.Left - 1;
this.Width = bounds.Width;
this.Top = bounds.Top;
this.Height = bounds.Height;

so the MediaElement has 1 pixel wide overlapped on the primary monitor, and then it's able to play H.264 video files normally.


3. Use another MP4 codec other than MS's Media Foundation codec

Download a tool "Win7DSFilterTweaker" to disable Media Foundation "MP4" playback. Install another MP4 codec, ffshow, for example.

Egon answered 10/5, 2012 at 17:16 Comment(3)
In this post, you quote something from MS. What is the reference for that quote? Do you know if this has been fixed in .NET 4.5.x?Pastoral
Apparently not fixed in .NET 4.5.x -- I'm coming up against this very problem now.Thenceforth
Apparently not fixed in .NET 6Rachele
B
1

Check if events: MediaOpened, MediaEnded and MediaFailed are still being raised. I assume not as this is a known issue that this control "ignores" the second monitor.

Bettinabettine answered 5/1, 2011 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.