问题描述
所以我已经有一段时间为这个问题挡在墙上了。
我的要求是在 wpf 应用程序中播放 .mp4
视频文件。听起来很简单,对吧?
起初,我使用内置的 MediaElement
类并且一切正常。
但事实证明,MediaElement
是基于 Windows Media Player
,对于使用 Windows 10 Pro N (or KN)
版本的人(包括我自己)来说,这些版本基本上是 Windows 10 的精简版,视频未播放,因为未包含 Windows Media Player
。
这也是通过将错误回调附加到 MediaElement
来报告的,导致消息:
"Windows Media Player version 10 or later is required"
当然,一种选择是向用户显示一条消息和一个下载功能包 (https://www.microsoft.com/en-us/software-download/mediafeaturepack) 的链接,其中包括 Windows Media Player
,但在我使用 Windows 应用程序的这些年里,我从来没有看到任何应用程序做/问。这也是错误的 - 功能包不仅仅是 WMP,用户可能不希望使用不必要的应用程序来膨胀他们的系统。这就是用户首先选择 Windows 10 Pro N (or KN)
的原因。
但是 - 其他应用程序是如何做到的?如何在我的 Windows 10 Pro N
上播放 Slack、Discord 应用上的视频?
一些谷歌搜索和通用解决方案是使用 VideoLAN.LibVLC.Windows
框架,因为它是开源的、维护良好的并且多年来已在多个平台上证明了自己。
好的,我知道它是 nuget 包,而且由于它是本机代码,我还下载了一个包装库以在 WPF 中使用它 - LibVLCSharp
和 LibVLCSharp.WPF
。
我已经成功集成了视频播放,在调试版本中一切正常。
现在是在发布版本中对其进行测试的时候了。我创建一个新的 Setup project
,构建它,安装它。
问题来了:
我的应用运行得很好,但是当我到达应该播放视频的部分时,出现异常错误:
Failed to load required native libraries.
Have you installed the latest LibVLC package from nuget for your target platform?
Search paths include
C:\Program Files (x86)\My app\libvlc\win-x64\libvlc.dll,C:\Program Files (x86)\My app\libvlc\win-x64\libvlccore.dll;
C:\Program Files (x86)\My app\libvlc\win-x64\libvlc.dll,C:\Program Files (x86)\My app\libvlc\win-x64\libvlccore.dll;
C:\Program Files (x86)\My app\libvlc.dll,
因此,在我的安装目录 C:\Program Files (x86)\My app\
中,我找到了以下 2 个 dll:
LibVLCSharp.dll
LibVLCSharp.WPF.dll
我检查了 Debug 版本的文件夹(因为它在那里工作得很好),我看到我还有一个 libvlc
文件夹正在那里生成,内容如下:
libvlc
- win-x64
-- hrtfs
-- locale
-- lua
-- plugins
-- libvlc.dll
-- libvlc.lib
-- libvlccore.dll
-- libvlccore.lib
- win-x86
-- hrtfs
-- locale
-- lua
-- plugins
-- libvlc.dll
-- libvlc.lib
-- libvlccore.dll
-- libvlccore.lib
我将该文件夹复制到我的安装目录,现在正在播放视频。 好的,我可以在安装过程中自动复制该文件夹。
但现在的问题是尺寸。
我的应用大小为 10 MB。该 libvlc
文件夹的大小为 324 MB。
一个 10 MB 的小应用程序突然增长到 334 MB,这是非常不可接受的。我会理解增加 10-20 MB,但不会增加这么多。
我了解,我可以创建两个单独的安装 - 一个用于 x64
,另一个用于 x86
系统。
太好了,大小减半。
然后通过反复试验,我从那些文件夹中删除了我的应用可以在没有的情况下继续播放视频的内容。
我剩下这个结构,它运行良好:
libvlc
- win-x64
-- plugins
-- libvlc.dll
-- libvlccore.dll
但它仍然重 125 MB。如果我从 plugins 文件夹中删除任何内容,应用程序就会崩溃。
是否有更好/正确的方法将这个“libvlc”文件夹包含到我的发布版本中,而不会影响我的安装程序大小?
也许还有另一个更轻量级的框架可以播放 .mp4
文件?
我是不是跳错了兔子洞?
====================== 更新 ====================== =
已解决,感谢 @cube45 建议。
确实,我可以删除不需要的插件,从而节省空间(我最初删除了一个必需的插件,所以我认为所有插件都是必需的,只是不走运)。
同样,通过反复试验,我只留下了成功显示视频所需的那些文件。
结果列表:
libvlc
- win-x64
-- libvlc.dll
-- libvlccore.dll
-- plugins
--- access
---- libimem_plugin.dll
--- codec
---- libavcodec_plugin.dll
---- libd3d11va_plugin.dll
--- video_output
---- libdirect3d9_plugin.dll
---- libdirect3d11_plugin.dll
---- libdrawable_plugin.dll
---- libvmem_plugin.dll
同样适用于 x86 配置。
主要问题,为什么没有在安装目录中生成 libvlc
文件夹,是因为我需要为该目录显式添加内容输出。
我这样做了:
1. Right clicked on **my setup project -> View -> File System**
2. Right clicked on **Application Folder -> Add -> Project Output**
3. Selected **Content Files -> Ok**
瞧 - 现在 libvlc
文件夹及其内容包含在安装程序文件中并放置在安装目录中。
但是,即使是压缩的,安装程序文件现在也有 140 MB 大,因为它包含整个 libvlc
文件夹。我们只需要包含我们需要的那些文件并跳过其余的。
这就是 @cube45 提供的链接派上用场的地方。
进程,我们只选择那些我们需要的文件/文件夹,称为 Cherry picking
。所以我在 *.csproj
文件中添加了以下内容:
<ItemGroup>
<VlcWindowsX64IncludeFiles Include="libvlc.dll" />
<VlcWindowsX64IncludeFiles Include="libvlccore.dll" />
<VlcWindowsX64IncludeFiles Include="plugins\access\libimem_plugin.dll" />
<VlcWindowsX64IncludeFiles Include="plugins\codec\libavcodec_plugin.dll" />
<VlcWindowsX64IncludeFiles Include="plugins\codec\libd3d11va_plugin.dll" />
<VlcWindowsX64IncludeFiles Include="plugins\video_output\libdirect3d9_plugin.dll" />
<VlcWindowsX64IncludeFiles Include="plugins\video_output\libdirect3d11_plugin.dll" />
<VlcWindowsX64IncludeFiles Include="plugins\video_output\libdrawable_plugin.dll" />
<VlcWindowsX64IncludeFiles Include="plugins\video_output\libvmem_plugin.dll" />
<VlcWindowsX86IncludeFiles Include="libvlc.dll" />
<VlcWindowsX86IncludeFiles Include="libvlccore.dll" />
<VlcWindowsX86IncludeFiles Include="plugins\access\libimem_plugin.dll" />
<VlcWindowsX86IncludeFiles Include="plugins\codec\libavcodec_plugin.dll" />
<VlcWindowsX86IncludeFiles Include="plugins\codec\libd3d11va_plugin.dll" />
<VlcWindowsX86IncludeFiles Include="plugins\video_output\libdirect3d9_plugin.dll" />
<VlcWindowsX86IncludeFiles Include="plugins\video_output\libdirect3d11_plugin.dll" />
<VlcWindowsX86IncludeFiles Include="plugins\video_output\libdrawable_plugin.dll" />
<VlcWindowsX86IncludeFiles Include="plugins\video_output\libvmem_plugin.dll" />
</ItemGroup>
瞧——现在我的安装文件只有 23 MB,这是完全可以接受的!
哦,这个尺寸可以减少一半,因为这是为 Any CPU
配置而构建的。因此,如果我将此安装拆分为两个单独的安装,则重量将约为 11.5 MB(但随后会出现一系列全新的问题,因此我不会深入研究)。
长话短说 - 成功!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)