如何玩一个WPF声音文件资源声音文件、资源、WPF

2023-09-02 21:35:06 作者:________初晴

我想在我的WPF应用程序播放声音文件。目前,我有以下电话:

I am trying to play a sound file in my WPF application. Currently I have the following call:

private void PlaySound(string uriPath)
{
    Uri uri = new Uri(@"pack://application:,,,/Media/movepoint.wav");
    var player = new MediaPlayer();
    player.Open(uri);
    player.Play();
}

现在,如果我指定媒体/ movepoint.wav 作为生成操作内容并加载它作为一个相对或绝对文件路径正常工作,所以我怀疑这事做的包URI ,但我不能为我的生活搞清楚什么。

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.

我们的目标是把文件存储为,使其不会在输出目录中可用的资源。我可以提供无论是WAV副本或MP3复制。

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.

感谢提前,

杰森。

推荐答案

我想这与一个图像文件,其工作方式一样的声音文件尽可能的URI是担心,因为它只是一个资源。我用code以下基本上符合你所拥有的。

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.

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.

使用.NET Framework 4.0,VS2012。

Using .NET Framework 4.0, VS2012.

此链接给出了pretty的整个包计划的很好的说明东西。

This link gives a pretty good description of the whole "pack" scheme of things.

修改

更多研究这个话题似乎表明你想要做什么可能是不可能的音频或视频文件。以下摘录取自this MSDN页。

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.

尽管你可以声明这个类扩展的一个实例   应用程序标记语言(XAML),你不能加载和播放其媒体   不使用code。如果只播放媒体在XAML中,使用的MediaElement。   此外,如果您在声明XAML的情况下,唯一可行的用途是   填写属性元素语法球员属性。

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.

当你的应用程序分发媒体,则不能使用媒体   文件作为项目资源。在您的项目文件,则必须改为设置   媒体类型的内容,并设置CopyToOutputDirectory为   preserveNewest或始终。

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可以在两种不同模式下使用,这取决于是   驾驶玩家:独立模式或时钟模式。在独立   模式,在MediaPlayer类似于图像和媒体开   通过Open方法驱动器播放。在时钟模式下,   MediaPlayer的可以被认为是作为目标的动画,因此   这将有相应的时间线和时钟输入的时序   树控制播放。有关媒体模式的详细信息,请参阅   多媒体概述。

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的是从一个不同的MediaElement在于它不是一个   可以直接加入到一个用户界面(UI)控制   应用。要显示使用的MediaPlayer媒体装载,一VideoDrawing   或的DrawingContext必须使用。

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.