Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

c# - How to play a WPF Sound File resource

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();
}

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Using .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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...