C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,363 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,447 people online right now. Registration is fast and FREE... Join Now!




How to add sounds to your software.

 
Reply to this topicStart new topic

> How to add sounds to your software., Learn to add sounds to your buttons, labels, etc.

papuccino1
Group Icon



post 12 Jun, 2009 - 05:30 PM
Post #1


Hi there, in this tutorial you'll learn how to use sounds in your software. Let's get to it.

1. Open up Visual C# Express (or Visual Studio), and create a new Windows Form project. Name it SoundExercise.

2. Wait for the IDE to do it's loading and then click on your Form1 to select it. Press the F7 key to go into that Form1's code.

3. Add using System.Media; to the Form1's using statements. (This is so we can use the SoundPlayer class)

4. Create a SoundPlayer instance, and set the soundLocation to the ever-popular Windows "Ding" sound.
csharp
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

SoundPlayer My_JukeBox = new SoundPlayer(@"C:\WINDOWS\Media\ding.wav");
}


5. Go back to the design tab of your Form1 and add a Button to it from the toolBox.

6. Double click the button you just dragged. Whatever we type between those brackets in the button1.Click() event will run whenever we click the button. How cool is that!

7. Inside the click event, write this code:
csharp
private void button1_Click(object sender, EventArgs e)
{
My_JukeBox.Play();
}


8. Now every time you click on that button, the "Ding" sound will play. Pretty neat

----------------------------------------------------------------------------------------------------

But what happens when you don't want to write down a physical adress on a users machine but you instead want to use your own project resource? It's easy, and I'll show you how.

You just have to change the adress of the .wav file when instancing the SoundPlayer class!

1. Right click on your solution and "Add" a "New Item" and select a "ResourceFile". Leave the default name

2. Filter the resources by selecting Audio Files:
IPB Image

3. Click "Add Resource" and navigate to C:Windows:Media:tada.wav

Ok, we have a .wav file in our resources so now let's use a new SoundPlayer class with it.

4. Create a new SoundPlayer by using the following line of code.
csharp

SoundPlayer My_BetterJukeBox = new SoundPlayer(Resource1.tada);


5. Now go back to Form1 design view, and add a new button.

6. Double click the new button and program this in the Click_event:
csharp

My_BetterJukeBox.Play();


7. Now every time this button is clicked the resource .wav file will play.

This is particularly useful because now you don't have to manually type up where the resource is on your hard-drive. Only add it resources, and use it. Simple as that.

I hope you enjoyed this brief tutorial, if you have any questions please leave it in the comments area and I'll help you out.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

SixOfEleven
Group Icon



post 17 Jun, 2009 - 06:38 PM
Post #2
Nice tutorial. Look forward to seeing more!
Go to the top of the page
+Quote Post

Beethoven
*



post 18 Jun, 2009 - 06:37 AM
Post #3
yea gr8 ur just full of cool ideas

ps. Thanks again for your help with the draw and drag
Go to the top of the page
+Quote Post

Smurphy
Group Icon



post 18 Jun, 2009 - 07:20 AM
Post #4
Great tutorial. Going to use this right now. wink2.gif
Go to the top of the page
+Quote Post

vladvlk
*



post 2 Jul, 2009 - 06:04 AM
Post #5
The second part of this tutorial is exactly what I was looking for - the way how to play the wav sound embedded in the project.
However, when applying the code to my project, I cannot avoid the debugger requiring the parameter of the SoundPlayer class to be of 'string' type instead of 'byte[]'.
Any hint where I am wrong?

CODE

        using System.Media;

        private SoundPlayer myPlayer;

        public Form1()
        {
            InitializeComponent();
            myPlayer = new SoundPlayer(Resource1.MenuPop);
            myInit();
        }

       private void myInit()
       {
        //...
       }


        private void buttonRun_Click(object sender, EventArgs e)
        {
            runC++;
            myPlayer.Play();
            buttonRUN.Text = runC.ToString();


Thanks a mil for any kick in the right direction!
vl.

This post has been edited by vladvlk: 2 Jul, 2009 - 06:09 AM
Go to the top of the page
+Quote Post

papuccino1
Group Icon



post 5 Jul, 2009 - 09:19 AM
Post #6
The SoundPlayer class only works for WAV files. Is the MenuPop a WAV file? TRIPLE-CHECK that. biggrin.gif
Go to the top of the page
+Quote Post

lesPaul456
Group Icon



post 5 Jul, 2009 - 03:10 PM
Post #7
You can convert a byte array to a string using the Convert.ToBase64String() method.
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/7/09 08:25PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month