Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does WPF MediaElement not work on secondary monitor?

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

like image 550
grantnz Avatar asked Nov 15 '10 22:11

grantnz


2 Answers

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.

like image 91
detale Avatar answered Dec 07 '22 22:12

detale


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.

like image 35
brovar Avatar answered Dec 07 '22 23:12

brovar