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???

grantnz
  • 7,322
  • 1
  • 31
  • 38
  • Could it be that the secondary monitor is not accelerated by the GPU? Is this consistent across many PC's or just on one? – ChrisF Nov 15 '10 at 22:53
  • 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. – grantnz Nov 15 '10 at 23:15
  • WPFMediaKit also works on both monitors.... – grantnz Nov 15 '10 at 23:35
  • Found another laptop that doesn't have this problem. Never found a solution for my laptop. – grantnz Jan 18 '11 at 01:29
  • 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. – Jippers Feb 07 '12 at 20:43

2 Answers2

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.

NoWar
  • 36,338
  • 80
  • 323
  • 498
detale
  • 12,482
  • 4
  • 41
  • 42
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.

brovar
  • 811
  • 4
  • 10
  • 25