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:
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();
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.