0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
thim24
  • 624
  • 1
  • 5
  • 14
  • The issue with the "relative URL" you're seeking is that it'd be relative _to what_? This is a compiled application, not a web page, so neither `Uri` nor `MediaPlayer` can assume the answer to that (particularly since it appears you're hosting a WPF control in WinForms). That you don't want to hard-code an absolute URL doesn't mean you can't construct one (path to `Debug` + `BackgroundMusicMix.wav`) at runtime. – Lance U. Matthews Jun 28 '20 at 19:38
  • @BACON It'd be relative to the application itself, but I understand what you're saying. But how would you code the path to debug then? – thim24 Jun 28 '20 at 19:46
  • 1
    `Assembly.GetExecutingAssembly().Location` will get you the absolute path to the `.exe` file, then `Path.GetDirectoryName()` to get the path of the containing directory, then `Path.Combine()` with `"BackgroundMusicMix.wav"` to get the sound file path, then create a `Uri` out of that. – Lance U. Matthews Jun 28 '20 at 19:55
  • string fullURI = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "BackgroundMusicMix.wav"); // this code seems to be working. thx! – thim24 Jun 28 '20 at 22:48

1 Answers1

1

The issue with the "relative URL" you're seeking is that it'd be relative to what? This is a compiled application, not a web page, so neither Uri nor MediaPlayer can assume the answer to that.

That you don't want to hard-code an absolute path doesn't mean you can't construct one at runtime, though. You can use the Assembly.GetExecutingAssembly() method to get the absolute path to your application...

string executableFilePath = Assembly.GetExecutingAssembly().Location;

...and then use the Path class to turn that into an absolute path to your audio file...

string executableDirectoryPath = Path.GetDirectoryName(executableFilePath);
string audioFilePath = Path.Combine(executableDirectoryPath, "BackgroundMusicMix.wav");

...from which you can create an absolute file URL...

Uri audioFileUri = new Uri(audioFilePath);

See Convert file path to a file URI? for some of the peculiarities to consider when constructing a Uri from a filesystem path like that.

By the way, as far as alternatives, I've not used this myself so I don't know how suited it is for what you're doing, but I know NAudio is a thing that exists.

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68