2

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.

Joshua
  • 40,822
  • 8
  • 72
  • 132
Simon Brolsma
  • 31
  • 1
  • 1
  • 3

2 Answers2

3

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();
Fahmi Noor Fiqri
  • 441
  • 7
  • 15
1

First add your resource. Then in the Solution Explorer click on it. In the Properties Panel, set Build Action to resource.

Next access it like this:

player.Stream = new MemoryStream(Properties.Resources.gameMusic);
Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 6,131
  • 5
  • 31
  • 52