I'm developing a retro-style game in C#
.NET-Framework
, and I would like to use different sounds and music in my game. But I have a problem. The original System.Windows.Media.SoundPlayer doesn't support 2 (or more) sounds being played. When one starts, it stops the active one.
I'm looking for a solution that can play different audio at the same time. I tried threading the different SoundPlayers to different threads, but that wasn't a good solution for me (+ it didn't work).
I read about System.Windows.Media.MediaPlayer, and the different controls that you could use with it. This instantly had me interested, especially after I read that you could play different sounds at once.
But trying to use the MediaPlayer in my game, it throws an error, because the URL isn't spelled correct. Here is my code:
using System.Windows.Media;
MediaPlayer Sound = new MediaPlayer();
MediaPlayer BackgroundMusic = new MediaPlayer();
private void Form1_Load(object sender, EventArgs e)
{
Sound.Open(new Uri("Text1.wav"));
BackgroundMusic.Open(new Uri("BackgroundMusicMix.wav"));
}
private void BtnSound_Click(object sender, EventArgs e)
{
Sound.Play();
}
private void BtnBackgroundSound_Click(object sender, EventArgs e)
{
BackgroundMusic.Play();
}
The .wav-files are here located in the \bin\debug
folder of my solution, because the SoundPlayer also gets it's sounds from there. I am aware of the fact that you can always put in the full filepath, but the project is being edited by multiple people, so we need the sounds to be located in the solution folder (relative URL).
SO my question is: what is the correct spelling for relative URL? Or even better, is there a simpler method to play 2 sounds simultaneously?