Playing Multiple Sounds in WPF Application

Mark Harwood
3 min readJul 27, 2021

So I’m making a small WPF game, which you can find here by the way ✨, and I come across a confusing and infuriating issue. WPF has two built-in audio players: SoundPlayer and MediaPlayer.

  • SoundPlayer: Can play audio files that are embedded resources in the application, however, this can only play one sound at once.
  • MediaPlayer: Cannot play audio files that are embedded resources in the application, however, this can play multiple sounds at once.

I think you can see the issue I was having. I wanted to play some background music in my game as well as some sound effects for certain actions in the game. This meant the easiest way to get this functionality was to use both of these options and save the background music to the local disk as a temporary file. Not as clean as I wanted, but it will do I guess 🤷‍♂️

First, I set up my SoundPlayer as this was the simplest one of the two. I created a new SoundPlayer object using the embedded resource:

private static readonly SoundPlayer _soundOne = new SoundPlayer(WPF.Properties.Resources.soundOne);

Then when I wanted this sound to play, I would simply use:

_soundOne.Play();

This is used as a temporary sound effect. In the game, it’s used as the ‘smacking’ sound effect.

Simple enough for the SoundPlayer… Now we move on to the MediaPlayer.

--

--

Mark Harwood

IT Pro 👨‍💻 Dad 👀 Husband🔒 Check out my free blog or utilise my expertise on the web! https://mharwood.uk