使用DLL资源WPF的MediaPlayer [英] Use DLL resources for WPF MediaPlayer

查看:191
本文介绍了使用DLL资源WPF的MediaPlayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目,我有一个DLL对于一些需要播放一些音频文件,WPF / XAML控制。现在,随着 声音播放 我可以让这些音频文件嵌入资源的DLL(即同时包含WPF控件)。然而,声音播放有一些严格的限制,所以我切换到的 的MediaPlayer

In my project I have a DLL for some WPF/XAML controls that need to play some audio files. Now, with SoundPlayer I can make these audio files "Embedded Resources" of the DLL (that also contains the WPF controls). However, SoundPlayer has some severe limitation so I switched to MediaPlayer.

不幸的是,的MediaPlayer 帮助页面指出的MediaPlayer 不能与资源工作。 (我尝试使用包的URI 的,但无论如何,这并没有工作 - 在至少没有一个组合我试过)。所以,现在,我正在写的DLL资源(音频文件),临时文件,然后使用它们的MediaPlayer ,但是这恕我直言不是一个好的解决方案。

Unfortunately, the MediaPlayer help page states that MediaPlayer can't work with resources. (I tried using Pack URIs anyway but this didn't work - at least none of the combinations I've tried.) So, for now, I'm writing the DLL resources (audio files) to temporary files and then use them with MediaPlayer, but that's IMHO not a "good" solution.

所以,我在想,如果有一个WPF一个正确的方式与的MediaPlayer 放置在一个DLL项目的音频文件。

So, I was wondering if there is a "correct" way in WPF with MediaPlayer to place audio files in a DLL project.

帮助页面指出(音频)文件应该被标记为内容,并使用复制到输出目录,但显然这是EXE项目的一部分,这仅适用于文件。它不为一个DLL项目工作。

The help page states that the (audio) files should be marked as "Content" and use "Copy to Output Directory" but obviously this only works for file that are part of the EXE project. It doesn't work for a DLL project.

推荐答案

当你发现的MediaPlayer无法从资源支持装入介质。

As you discovered MediaPlayer does not support loading media from resources.

最简单的方法是分发声音文件与应用程序,并将其放置在应用程序文件夹 - 内容/复制到输出目录做到了这一点。

The simplest option is to distribute the sound files with your application and place them in the application folder - Content/"Copy to Output Directory" does just that.

如果你打的Visual Studio生成系统的limitaiton你可以将文件复制自己(拖/拖放到bin / Debug或斌/的EXE项目的发布folser)或写,将它们复制一个批处理文件,并用它作为生成后的行动。

If you hit a limitaiton of Visual Studio build system you can just copy the files yourself (drag/drop them into the bin/Debug or bin/Release folser of the exe project) or write a batch file that will copy them and use it as a post-build action.

显然,当你船的应用程序了给最终用户,你必须确保文件正确复制。

Obviously, when you ship the applicaiton to an end user you have to make sure the files are copied correctly.

如果你正在写一个DLL使用其他develpers,你不想手动文件复制是开发商的安装过程中可以随时使用指向文件位置的注册表项(通过DLL的设置安装程序) - 并退回到EXE文件夹,如果该注册表项丢失(这样EXE开发商仍然要捆绑的EXE文件,但并不需要乱用在开发过程中任何东西)

If you are writing a DLL to be used by other develpers and you don't want manual file copying to be part of the developer's installation process you can always use a registry key that points to the files location (set by the DLL's installer) - and fall back to the EXE folder if the registry key is missing (so the EXE developer still has to bundle the files with the EXE but doesn't need to mess with anything during development).

和,当然,如果你必须有DLL作为一个独立的文件包,你所剩下的只是你已经在使用的解压到临时文件夹选项。

And, of course, if you must have the DLL as a one file standalone package you are left only with the "extract to temp folder" option you already use.

这篇关于使用DLL资源WPF的MediaPlayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆