C# 如何播放 WPF 声音文件资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15145209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to play a WPF Sound File resource
提问by xximjasonxx
I am trying to play a sound file in my WPF application. Currently I have the following call:
我正在尝试在我的 WPF 应用程序中播放声音文件。目前我有以下电话:
private void PlaySound(string uriPath)
{
Uri uri = new Uri(@"pack://application:,,,/Media/movepoint.wav");
var player = new MediaPlayer();
player.Open(uri);
player.Play();
}
Now if I specify Media/movepoint.wav
as build action Content
and load it as a relative or absolute file path it works fine, so I suspect this has something to do with the Pack URI
, but I cannot for the life of me figure out what.
现在,如果我指定Media/movepoint.wav
为构建操作Content
并将其加载为相对或绝对文件路径,它可以正常工作,所以我怀疑这与Pack URI
.
The objective is to store the file as a resource so that its not available in the output directory. I can provide either the WAV copy or the MP3 copy.
目标是将文件存储为资源,使其在输出目录中不可用。我可以提供 WAV 副本或 MP3 副本。
采纳答案by Stewbob
I tried this with an image file, which works the same as a sound file as far as the uri is concerned because it's just another resource. I used the code below which essentially matches what you have.
我用一个图像文件尝试了这个,就 uri 而言,它与声音文件的工作原理相同,因为它只是另一种资源。我使用了下面的代码,它基本上与您所拥有的相匹配。
new Uri(@"pack://application:,,,/Resources/logo.png")
Make sure that your 'Media' folder is not nested in any other folder. If it is, you need to include that folder as well.
确保您的“媒体”文件夹未嵌套在任何其他文件夹中。如果是,您还需要包含该文件夹。
Using .NET Framework 4.0, VS2012.
使用 .NET Framework 4.0,VS2012。
This linkgives a pretty good description of the whole "pack" scheme of things.
这个链接很好地描述了整个“打包”方案。
EDIT
编辑
More research on this topic seems to indicate that what you want to do might not be possible with audio or video files. The excerpt below is taken from the remarks section of this MSDN page.
关于此主题的更多研究似乎表明您想要做的事情可能无法使用音频或视频文件。下面的摘录摘自此 MSDN 页面的备注部分。
Although you can declare an instance of this class in Extensible Application Markup Language (XAML), you cannot load and play its media without using code. To play media in XAML only, use a MediaElement. Also, if you declare an instance in XAML, the only practical use is to fill property element syntax for the Player property.
When distributing media with your application, you cannot use a media file as a project resource. In your project file, you must instead set the media type to Content and set CopyToOutputDirectory to PreserveNewest or Always.
MediaPlayer can be used in two different modes, depending on what is driving the player: independent mode or clock mode. In independent mode, the MediaPlayer is analogous to an image and the media opened through the Open method drives playback. In Clock mode, the MediaPlayer can be thought of as a target for an animation, and thus it will have corresponding Timeline and Clock entries in the Timing tree which controls playback. For more information on media modes, see the Multimedia Overview.
MediaPlayer is different from a MediaElement in that it is not a control that can be added directly to the user interface (UI) of an application. To display media loaded using MediaPlayer, a VideoDrawing or DrawingContext must be used.
尽管您可以在可扩展应用程序标记语言 (XAML) 中声明此类的实例,但如果不使用代码,则无法加载和播放其媒体。若要仅在 XAML 中播放媒体,请使用 MediaElement。此外,如果您在 XAML 中声明一个实例,唯一的实际用途是填充 Player 属性的属性元素语法。
使用您的应用程序分发媒体时,您不能将媒体文件用作项目资源。在您的项目文件中,您必须将媒体类型设置为 Content,并将 CopyToOutputDirectory 设置为 PreserveNewest 或 Always。
MediaPlayer 可以在两种不同的模式下使用,具体取决于播放器的驱动因素:独立模式或时钟模式。在独立模式下,MediaPlayer 类似于图像,通过 Open 方法打开的媒体驱动播放。在时钟模式下,MediaPlayer 可以被认为是动画的目标,因此它在控制播放的时序树中会有相应的时间轴和时钟条目。有关媒体模式的更多信息,请参阅多媒体概述。
MediaPlayer 与 MediaElement 的不同之处在于它不是可以直接添加到应用程序的用户界面 (UI) 的控件。要显示使用 MediaPlayer 加载的媒体,必须使用 VideoDrawing 或 DrawingContext。
回答by dotNET
The following seems to work in .NET Framework 4.5:
以下似乎适用于 .NET Framework 4.5:
var sri = Application.GetResourceStream(new Uri("pack://application:,,,/MyAssemblyName;component/Resources/CameraShutter.wav"));
if ((sri != null))
{
using (s == sri.Stream)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(s);
player.Load();
player.Play();
}
}
CameraShutter.wav
is embedded as Resourcein my project (and resides inside Resources subfolder, as indicated in the pack URI).
CameraShutter.wav
在我的项目中作为Resource嵌入(并驻留在 Resources 子文件夹中,如包 URI 所示)。
回答by VDWWD
You can also load a Stream into the SoundPlayer if the .wav file is an Embedded Resource. Note that in this example the resources are in a folder called Resources
that is in the root of the project, that is why it is written {0}.Resources.{1}
.
如果 .wav 文件是嵌入式资源,您还可以将 Stream 加载到 SoundPlayer 中。请注意,在此示例中,资源位于Resources
项目根目录中名为{0}.Resources.{1}
.
//the wav filename
string file = "emergency_alarm_002.wav";
//get the current assembly
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
//load the embedded resource as a stream
var stream = assembly.GetManifestResourceStream(string.Format("{0}.Resources.{1}", assembly.GetName().Name, file));
//load the stream into the player
var player = new System.Media.SoundPlayer(stream);
//play the sound
player.Play();