如何使 MediaElement 与 AxWindowsMediaPlayer 一样好用?
How to make MediaElement work as good as AxWindowsMediaPlayer?
我的 C# 应用程序中需要一个非常简单的视频播放器。它只需要从文件中循环播放视频,仅此而已。由于我正在开发 WPF 应用程序,因此我尝试使用 System.Windows.Controls.MediaElement
。它具有我需要的所有功能,但效果很差:我在上面播放了一些全高清视频,但它总是滞后和尖峰。
为确定这不是我的应用程序问题,我创建了 2 个测试应用程序。第一个 WinForms
无边框 1920x1080 window 只有 AxWMPLib.AxWindowsMediaPlayer
控件。第二个无边框 WPF
window 大小与 System.Windows.Controls.MediaElement
.
相同
然后我 运行 两个播放器上的 2 个视频。这是他们的规格:
1: 1920x1080, 12000kb/s, 25 FPS, wmv
2: 1920x1080, 5730kb/s, 25 FPS, mp4
在 AxWindowsMediaPlayer
上一切正常。但是 MediaElement
似乎丢掉了一些帧并忽略了垂直同步(在快速场景变化期间,可能会在另一帧上看到一帧的部分内容)。所以,这是完全不合适的,不应该那样的,但我在微软官方文档中没有发现任何关于这个问题的信息(他们只建议在 WPF
中使用 MediaElement
而不是 AxWindowsMediaPlayer
应用)。是否有可能让它工作得更顺畅,或者使用额外的 WinForms
Form
和 AxWindowsMediaPlayer
是唯一的解决方案?
It was written over five years ago(查看线程中的 James Dailey 消息),可能有一些改进,但总的来说,我认为这些声明仍然信誉良好。我会挑一些相关的引语:
As you know the WPF environment is constructed from the ground up to offer developers a very rich “graphics first” environment. The MediaElement in particular was designed to allow you to mix video with various other UI components seamlessly. This solution will give you the flicker free, “draw over video” solution that you are looking for. The best part is you can do all of this in C#. The bad part of this solution is that the MediaElement is not designed for displaying time sensitive media content. In other words, the MediaElement is notorious for dropping and delaying the display of video frames. There are ways to minimize this such as using SD rather than HD content, use a video accelerated codec, etc.
还有:
Unfortunately you can’t really tell the WPF MediaElement to never drop frames. The term we use for this class of issues is “disparate clocks”. In this case WPF is updating the screen at a certain rate (clock 1). The MediaElement (based on WMP) is cranking out video frames at a slightly different rate (clock 2). Given the underlying technologies there is currently no way to synchronize the two clocks and force them to “tick” at the same rate. Since the display will only be updated according to the WPF clock, multiple frames of video may be sent from the MediaElement to WPF between clock ticks. Because of this the MediaElement may appear to drop frames. This is a very common problem in multimedia development and there is no simple solution.
Windows Media Player 使用 Media Foundation 和 DirectShow API,为媒体播放提供高质量的视频体验。
我的 C# 应用程序中需要一个非常简单的视频播放器。它只需要从文件中循环播放视频,仅此而已。由于我正在开发 WPF 应用程序,因此我尝试使用 System.Windows.Controls.MediaElement
。它具有我需要的所有功能,但效果很差:我在上面播放了一些全高清视频,但它总是滞后和尖峰。
为确定这不是我的应用程序问题,我创建了 2 个测试应用程序。第一个 WinForms
无边框 1920x1080 window 只有 AxWMPLib.AxWindowsMediaPlayer
控件。第二个无边框 WPF
window 大小与 System.Windows.Controls.MediaElement
.
然后我 运行 两个播放器上的 2 个视频。这是他们的规格:
1: 1920x1080, 12000kb/s, 25 FPS, wmv
2: 1920x1080, 5730kb/s, 25 FPS, mp4
在 AxWindowsMediaPlayer
上一切正常。但是 MediaElement
似乎丢掉了一些帧并忽略了垂直同步(在快速场景变化期间,可能会在另一帧上看到一帧的部分内容)。所以,这是完全不合适的,不应该那样的,但我在微软官方文档中没有发现任何关于这个问题的信息(他们只建议在 WPF
中使用 MediaElement
而不是 AxWindowsMediaPlayer
应用)。是否有可能让它工作得更顺畅,或者使用额外的 WinForms
Form
和 AxWindowsMediaPlayer
是唯一的解决方案?
It was written over five years ago(查看线程中的 James Dailey 消息),可能有一些改进,但总的来说,我认为这些声明仍然信誉良好。我会挑一些相关的引语:
As you know the WPF environment is constructed from the ground up to offer developers a very rich “graphics first” environment. The MediaElement in particular was designed to allow you to mix video with various other UI components seamlessly. This solution will give you the flicker free, “draw over video” solution that you are looking for. The best part is you can do all of this in C#. The bad part of this solution is that the MediaElement is not designed for displaying time sensitive media content. In other words, the MediaElement is notorious for dropping and delaying the display of video frames. There are ways to minimize this such as using SD rather than HD content, use a video accelerated codec, etc.
还有:
Unfortunately you can’t really tell the WPF MediaElement to never drop frames. The term we use for this class of issues is “disparate clocks”. In this case WPF is updating the screen at a certain rate (clock 1). The MediaElement (based on WMP) is cranking out video frames at a slightly different rate (clock 2). Given the underlying technologies there is currently no way to synchronize the two clocks and force them to “tick” at the same rate. Since the display will only be updated according to the WPF clock, multiple frames of video may be sent from the MediaElement to WPF between clock ticks. Because of this the MediaElement may appear to drop frames. This is a very common problem in multimedia development and there is no simple solution.
Windows Media Player 使用 Media Foundation 和 DirectShow API,为媒体播放提供高质量的视频体验。