What's new

.NET How start music when people open tool

  • Thread starter LAMENTOOL MODZ
  • Start date
  • Views 296
L

LAMENTOOL MODZ

Enthusiast
Messages
317
Reaction score
61
hi to all , i need code for start music when people open program , can anyone show me please ?
 
I

ImOx

(^._.^)ノ
Retired
Messages
9,968
Reaction score
2,963
You could to this

C:
using System.Media;

SoundPlayer Meow = new SoundPlayer(@"c:\Music\KittensAndCatsMeowing.wav");
Meow.Play();//have this on form_load
 
X

Xeren

♦♦♦ God Complex ♦♦♦
Messages
5,671
Reaction score
2,120
You could to this

C:
using System.Media;

SoundPlayer Meow = new SoundPlayer(@"c:\Music\KittensAndCatsMeowing.wav");
Meow.Play();//have this on form_load
That's C#, not C or C++.

hi to all , i need code for start music when people open program , can anyone show me please ?
You'll obviously have to resort to an external library of files to play any audio. You could try SDL or PortAudio(I believe that's the name).
 
X

Xeren

♦♦♦ God Complex ♦♦♦
Messages
5,671
Reaction score
2,120
There is no thread prefix for C#, so I'm almost positive he means C#.
C# is part of the .NET framework, which there is a prefix for. It is true though that they're mixed up from time to time.
 
I

ImOx

(^._.^)ノ
Retired
Messages
9,968
Reaction score
2,963
C# is part of the .NET framework, which there is a prefix for. It is true though that they're mixed up from time to time.
Yeah, but determining from the question he asked here, I don't think he knew that.
 
L

LAMENTOOL MODZ

Enthusiast
Messages
317
Reaction score
61
You could to this

C:
using System.Media;

SoundPlayer Meow = new SoundPlayer(@"c:\Music\KittensAndCatsMeowing.wav");
Meow.Play();//have this on form_load
the (@c:\music\etc tec .wav) is the file inside the folder of tool , true ?
 
C

Chris7S

Nerd by definition, programmer by trade
Messages
1,507
Reaction score
490
the (@c:\music\etc tec .wav) is the file inside the folder of tool , true ?
The (@c:\music\etc tec .wav) is a verbatim string literal which denotes the path of the file. So based on the string you have listed that is not getting the file from the application's folder. That is retreiving it from the C:\ drive and in the folders listed after that. If the audio file is located in the location of your application then you can write your code like so to make it short and sweet and more universal:

Code:
SoundPlayer player = new SoundPlayer(Application.StartupPath + "\file.wav");
player.Play();
 
Last edited:
M

MatthewH

Member
Messages
1,604
Reaction score
629
First off, let me tell you. If I were you, I wouldn't add music to it. I've personally always hated tools like that and proceed quickly to exit and delete the tool because it's ridiculous and just stupid. That's only my opinion though. Do what you wish.

Now, to how you should do it. First...don't reference local file...bad habit. Why? Because people can easily modify the file or replace it. What if you don't want that file changed?

Add it as a resource. (Settings/Properties > Resources I believe). Now, before you do anything remember to import System.Media for SoundPlayer.
Next the code...
Code:
using System.Media;

SoundPlayer music = new SoundPlayer(Properties.Resources.file_name_and_such);
music.PlayLooping();

I believe that's it. I'm doing it from memory so if I'm wrong, someone please correct me.
 
C

Chris7S

Nerd by definition, programmer by trade
Messages
1,507
Reaction score
490
First off, let me tell you. If I were you, I wouldn't add music to it. I've personally always hated tools like that and proceed quickly to exit and delete the tool because it's ridiculous and just stupid. That's only my opinion though. Do what you wish.

Now, to how you should do it. First...don't reference local file...bad habit. Why? Because people can easily modify the file or replace it. What if you don't want that file changed?

Add it as a resource. (Settings/Properties > Resources I believe). Now, before you do anything remember to import System.Media for SoundPlayer.
Next the code...
Code:
using System.Media;

SoundPlayer music = new SoundPlayer(Properties.Resources.file_name_and_such);
music.PlayLooping();

I believe that's it. I'm doing it from memory so if I'm wrong, someone please correct me.
That works as well. The embedding of the file in the .exe is completely optional though.
 
Top Bottom