Add multimedia content to an application in WPF

May include but is not limited to: media player vs. media element; adding a sound player; images

The .NET Framework is baaad at media support. When I tried to stick together a simple application in 2.0 which managed sounds, I had to find out that it’s not built with that in mind. However, WPF has far more capabilities then did Windows Forms, so let’s find out how to create hardcore visual and audio effects!

The first thing you can try is the SoundPlayer. You won’t like it, neither did I. It can only play .wav files (and only in pcm format). Anyway, if you are the pcm wav player guy then you could do as follows:

SoundPlayer sp = new SoundPlayer();
sp.SoundLocation = “my.wav”;

This will work if you have a file named my.wav (in pcm wav format) in the application folder, set with the Build Action Content, and always copied to the output. Fail to do that and the code will fail nicely, too. You can use the Stream property of the SoundPlayer if you set the build action of your wav file to Embedded Resource – and retrieve the binary resource stream by hand.

OK, SoundPlayer is limited, can’t handle big files (it must first read them into memory), don’t support multiple file playback, and it’s just crappy, so there’s a much better alternative called SoundPlayerAction. It has the funny name because it derives from TriggerAction (which means that you can use it as a response for events – more info on that in the Trigger post later). One further advance is that you don’t have to write code to handle it. Consider the following sample:

    <EventTrigger RoutedEvent=”Button.Click”>
     <SoundPlayerAction Source=”my.wav”/>

There is one other way to play sounds – the SystemSounds class. But you are very limited in what you can do: Asterisk, Beep, Exclamation, Hand and Question sounds are what you can play.

So want to play anything other than wav? I don’t know why would you do such a fool thing, but you can do it, if you insist. There are of course more than one way to skin a cat – we have MediaPlayer and MediaElement. Let’s start  with the former.

MediaPlayer is much like SoundPlayer – you load a file and play it back. MediaPlayer only supports asynchronous playback. Here’s a simple demo:

MediaPlayer mp = new MediaPlayer();
mp.Open(Uri yourUri);

OK, there’s more in this – first there are no exceptions involved in MediaPlayer, so you’ll have to sign up for events to know if something went wrong. These events are MediaOpened and MediaFailed. But you can play back MP3! I hope you feel honored. And there are some properties you can change as well like Balance, Volume, SpeedRatio and Position. Also there are properties like DownLoadProgress and BufferingProgress (starting to get the whole picture don’t you).

So much for MediaPlayer. The real hard core stuff in WPF is the MediaElemet. It can even play video… And it doesn’t really need code (but doesn’t throw exceptions as well, so prepare for handling MediaFailed events). In its simplest form, MediaElement is like this:

<MediaElement Source=”my.mp3” />

Should you play video files; the video will appear where you placed the MediaElement tag in the XAML markup – pretty straightforward. One thing not so straightforward: if you’d like to play a video (or audio) file programmatically, you should set the MediaElement’s LoadBehavior property to Manual, and then fire up the video in your code.

3 thoughts on “Add multimedia content to an application in WPF

  1. I have no problem playing a video file in my WPF app using as long as I include the path to where that video is located on my hard drive. But when I publish the app, it does not include that video in the published app. I’ve tried adding the video into my project as a resource…still no luck.
    Can you offer any solution? Thanks in advance…


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s