How to put music in console application with C#


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


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 {
} catch (Exception ex) {
    Console.WriteLine(ex.Message); //Somente um exemplo exibir erros em caso de falha, pode modificar como desejar
Scroll to Top