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指定为构建操作内容并将其作为相对或绝对文件路径加载它工作正常,所以我怀疑这与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副本。
2 个解决方案
#1
10
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.
确保“Media”文件夹未嵌套在任何其他文件夹中。如果是,您还需要包含该文件夹。
Using .NET Framework 4.0, VS2012.
使用.NET Framework 4.0,VS2012。
This link gives 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.
虽然您可以在可扩展应用程序标记语言(XAML)中声明此类的实例,但是如果不使用代码,则无法加载和播放其媒体。要仅在XAML中播放媒体,请使用MediaElement。此外,如果在XAML中声明实例,唯一的实际用途是填充Player属性的属性元素语法。
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.
在使用应用程序分发媒体时,不能将媒体文件用作项目资源。在项目文件中,您必须将媒体类型设置为Content,并将CopyToOutputDirectory设置为PreserveNewest或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可以在两种不同的模式下使用,具体取决于驱动播放器的内容:独立模式或时钟模式。在独立模式下,MediaPlayer类似于图像,通过Open方法打开的媒体驱动播放。在时钟模式下,MediaPlayer可以被认为是动画的目标,因此它将在时序树中具有相应的时间轴和时钟条目来控制回放。有关媒体模式的更多信息,请参阅多媒体概述。
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.
MediaPlayer与MediaElement的不同之处在于它不是可以直接添加到应用程序的用户界面(UI)的控件。要显示使用MediaPlayer加载的媒体,必须使用VideoDrawing或DrawingContext。
#2
1
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 Resource in my project (and resides inside Resources subfolder, as indicated in the pack URI).
CameraShutter.wav作为资源嵌入到我的项目中(并驻留在Resources子文件夹中,如包URI中所示)。
#1
10
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.
确保“Media”文件夹未嵌套在任何其他文件夹中。如果是,您还需要包含该文件夹。
Using .NET Framework 4.0, VS2012.
使用.NET Framework 4.0,VS2012。
This link gives 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.
虽然您可以在可扩展应用程序标记语言(XAML)中声明此类的实例,但是如果不使用代码,则无法加载和播放其媒体。要仅在XAML中播放媒体,请使用MediaElement。此外,如果在XAML中声明实例,唯一的实际用途是填充Player属性的属性元素语法。
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.
在使用应用程序分发媒体时,不能将媒体文件用作项目资源。在项目文件中,您必须将媒体类型设置为Content,并将CopyToOutputDirectory设置为PreserveNewest或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可以在两种不同的模式下使用,具体取决于驱动播放器的内容:独立模式或时钟模式。在独立模式下,MediaPlayer类似于图像,通过Open方法打开的媒体驱动播放。在时钟模式下,MediaPlayer可以被认为是动画的目标,因此它将在时序树中具有相应的时间轴和时钟条目来控制回放。有关媒体模式的更多信息,请参阅多媒体概述。
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.
MediaPlayer与MediaElement的不同之处在于它不是可以直接添加到应用程序的用户界面(UI)的控件。要显示使用MediaPlayer加载的媒体,必须使用VideoDrawing或DrawingContext。
#2
1
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 Resource in my project (and resides inside Resources subfolder, as indicated in the pack URI).
CameraShutter.wav作为资源嵌入到我的项目中(并驻留在Resources子文件夹中,如包URI中所示)。