Why does WPF MediaElement not work on secondary monitor?

前端 未结 2 1749
执念已碎
执念已碎 2020-12-15 05:26

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 secondar

相关标签:
2条回答
  • 2020-12-15 06:25

    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.

    0 讨论(0)
  • 2020-12-15 06:28

    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.

    0 讨论(0)
提交回复
热议问题