How to put music in console application with C#

Question:

I'm creating a game in C# (console application) and I need to put a musical background, how do I do it?

Answer:

As an example in the MSDN documentation you can use something like:

It matters:

using System.Media;

And use it like this:

SoundPlayer player = new SoundPlayer();
player.SoundLocation = Environment.CurrentLocation + "typewriter.wav";

In this case you just need to set the location of the audio in SoundLocation and then run .Play (runs asynchronously, to load synchronously, which can crash the app depending on the case, use PlaySync ):

try {
    player.Play();
} catch (Exception ex) {
    Console.WriteLine(ex.Message); //Somente um exemplo exibir erros em caso de falha, pode modificar como desejar
}
Scroll to Top