How to play a WPF Sound File resource How to play a WPF Sound File resource wpf wpf

How to play a WPF Sound File resource


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.


The following seems to work in .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).


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

//the wav filenamestring file = "emergency_alarm_002.wav";//get the current assemblyvar assembly = System.Reflection.Assembly.GetExecutingAssembly();//load the embedded resource as a streamvar stream = assembly.GetManifestResourceStream(string.Format("{0}.Resources.{1}", assembly.GetName().Name, file));//load the stream into the playervar player = new System.Media.SoundPlayer(stream);//play the soundplayer.Play();