Why does WPF MediaElement not work on secondary monitor?

♀尐吖头ヾ 提交于 2019-11-28 23:13:42
detale

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!