• (2 Pages)
  • +
  • 1
  • 2

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

#1 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 62
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Posted 12 June 2009 - 06:30 PM

*
POPULAR

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.
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:
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:
Posted 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.
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:
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.

Is This A Good Question/Topic? 5
  • +

Replies To: How to add sounds to your software.

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Posted 17 June 2009 - 07:38 PM

Nice tutorial. Look forward to seeing more!
Was This Post Helpful? 1
  • +
  • -

#3 Beethoven  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-June 09

Posted 18 June 2009 - 07:37 AM

yea gr8 ur just full of cool ideas

ps. Thanks again for your help with the draw and drag
Was This Post Helpful? 0
  • +
  • -

#4 Smurphy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 367
  • Joined: 07-July 08

Posted 18 June 2009 - 08:20 AM

Great tutorial. Going to use this right now. ;)
Was This Post Helpful? 0
  • +
  • -

#5 vladvlk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-July 09

Posted 02 July 2009 - 07:04 AM

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?

		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: 02 July 2009 - 07:09 AM

Was This Post Helpful? 0
  • +
  • -

#6 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 62
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Posted 05 July 2009 - 10:19 AM

The SoundPlayer class only works for WAV files. Is the MenuPop a WAV file? TRIPLE-CHECK that. :D
Was This Post Helpful? 0
  • +
  • -

#7 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Posted 05 July 2009 - 04:10 PM

You can convert a byte array to a string using the Convert.ToBase64String() method.
Was This Post Helpful? 0
  • +
  • -

#8 nathalie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-January 10

Posted 01 January 2010 - 06:36 PM

how would i set it for a MP3 format type?
and or make a MP3 format type file to a wave format?

This post has been edited by nathalie: 01 January 2010 - 06:41 PM

Was This Post Helpful? 0
  • +
  • -

#9 snoochy_dave  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-January 09

Posted 08 January 2010 - 06:06 AM

View Postnathalie, on 1 Jan, 2010 - 05:36 PM, said:

how would i set it for a MP3 format type?
and or make a MP3 format type file to a wave format?



http://www.nch.com.au/switch/ download the free trial and it will convert your audio files. Only just started using c# however one issue i ran into is that the soundplayer can only play PCM Wav files so make sure you set the encoding options to PCM.

Hope this helps
Was This Post Helpful? 0
  • +
  • -

#10 nathalie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-January 10

Posted 08 January 2010 - 05:10 PM

http://www.nch.com.au/switch/ download the free trial and it will convert your audio files. Only just started using c# however one issue i ran into is that the soundplayer can only play PCM Wav files so make sure you set the encoding options to PCM.

Hope this helps
[/quote]

thx
Was This Post Helpful? 0
  • +
  • -

#11 snoochy_dave  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-January 09

Posted 10 January 2010 - 12:44 PM

with regards to adding sounds... how do i get a sound to play twice?

SoundPlayer dogPlayer = new SoundPlayer(Properties.Resources.DogS);
dogPlayer.Play();
dogPlayer.Play();
dogPlayer.Play();

This is what i tried but it will only play once.

any thoughts?
Was This Post Helpful? 0
  • +
  • -

#12 foobar2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-February 10

Posted 02 February 2010 - 07:16 AM

nice, thanks for posting.
I also used the SoundPlayer for some times but it has some limitations, fortunately there are other libararies available, like for example irrKlang.
Like @snoochy_dave said, it's not possible to play a lot of sounds at the same time for example. Or to play .ogg files if the codec is not installed. irrKlang does this.
Was This Post Helpful? 0
  • +
  • -

#13 Cemgates101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-February 10

Posted 02 February 2010 - 09:43 AM

Thanks for the tutorial. It was easy and fun to do!
Was This Post Helpful? 0
  • +
  • -

#14 charqus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-February 10

Posted 07 February 2010 - 01:54 PM

Great tutorial , but i got a middle problem :/ .

At the 2nd part , that with load an extern *.wav

Look what i did:

1. I right clicked on solution name and i choose ADD - New Item - Resource File ( NAME: Resource1.resx )

2. I choosed Audio , then i pressed ADD RESOURCE , and i selected my WAV ( teapa.wav )
http://files.uploadf...3fb80f13/ss.PNG

3. I have created a new sound player
namespace Browser
{
    public partial class Form1 : Form
    {
        SoundPlayer Teapa = new SoundPlayer(Resource1.teapa);


4. I've made a button , i put the code:
Teapa.Play();


5. I pressed DEBUG , and when i press the Button , surprise:

http://files.uploadf...e/errbutton.PNG

and the .exe recive Don't Send

Thank you :tup:
Was This Post Helpful? 0
  • +
  • -

#15 cr4p  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-January 12

Posted 15 January 2012 - 12:20 PM

Hi all

1. thank you for the code
2. i followed your tutorial and succeeded
3. i want to exceed it as following:

i have ten buttons (labeled from 0...9) means button1.Text = 1 etc.
i have ten wav sounds in resource1.resx 0.wav ... 9.wav
i want to hear the sound 0.wav when button0 is pressed.

Here is a part of the code so far.

public Form1()
        {
            InitializeComponent();
        }
        SoundPlayer DialToneJukeBox = new SoundPlayer();

        private void button1_Click(object sender, EventArgs e)
        {
            DialToneJukeBox.Play(Resource1._1);
        }

etc...
But there is a fault (i am progging in german and try to translate the message into english)
No overload for the play-method takes 1 argument.

What is the right way to do it? I am no real noob to progging but quite new to OO.
Parallel i am working through literature but it is not easy to find hints.

Thank you very much in advance.

This post has been edited by Atli: 15 January 2012 - 12:26 PM
Reason for edit:: Added [code] tags.

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2