How to play background music in a C# Console Application

Simon Brolsma picture Simon Brolsma · Dec 6, 2015 · Viewed 26.7k times · Source

So I've made a small text-based game in C# but I'd like some music to it, I have tried this:

System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = "c:\PathToMusic\..music.wav";
player.Play();

Which worked, but when I build my application to a .exe file it's not included so when the other person does not have that specific sound in the specific folder it won't work.
I have added the file to my Solution Explorer but have no idea what that file location is called.

Answer

Fahmi Noor Fiqri picture Fahmi Noor Fiqri · Dec 6, 2015

Try this solution :

Add music file into Solution

Project -> Add Existing Item... -> Add your .wav file.

Set the file to Copy always

  1. Click the music file in Solution Explorer.
  2. In the Properties panel, set Copy to Output Directory to Copy always.
  3. Also, set Build Action to Content.

This will make Visual Studio always copying the music file into the output directory (e.g. Debug folder).

Use this code

using System.Media;  

SoundPlayer player = new SoundPlayer();
player.SoundLocation = AppDomain.CurrentDomain.BaseDirectory + "\\yourmusic.wav";
player.Play();