locked
Sound on a click event? RRS feed

  • Question

  • Hi, I am a student in highschool and for my final project I would like to make a Music program.
    Right now I would like to know how I would play a sound under the button_click event

    So if no one minds showing me the code  that would be awsome =)

    Thanks in advanced!
    Tuesday, December 4, 2007 5:22 PM

Answers

  • Add a using clause to System.Media. Then, in the click event of the button, use the following code (change waveFile to the media you want to play):

     

     

    Code Block

    string waveFile = @"c:\Windows\Media\chimes.wav";

    SoundPlayer sound = new SoundPlayer(waveFile);

    sound.Play();

     

     

    Tuesday, December 4, 2007 6:12 PM
  •  AllisDeviant wrote:
    Hi, I am a student in highschool and for my final project I would like to make a Music program.
    Right now I would like to know how I would play a sound under the button_click event

    So if no one minds showing me the code  that would be awsome =)

    Thanks in advanced!

     

    private void myButtonToPlayMyMusic_Click (object sender, EventArgs ee)

    {

    string action = "C:\whateverYourMusicFileIs.wav";

    System.Diagnostics.Process.Start ( action );

    }

    Tuesday, December 4, 2007 8:03 PM

All replies

  • Add a using clause to System.Media. Then, in the click event of the button, use the following code (change waveFile to the media you want to play):

     

     

    Code Block

    string waveFile = @"c:\Windows\Media\chimes.wav";

    SoundPlayer sound = new SoundPlayer(waveFile);

    sound.Play();

     

     

    Tuesday, December 4, 2007 6:12 PM
  •  AllisDeviant wrote:
    Hi, I am a student in highschool and for my final project I would like to make a Music program.
    Right now I would like to know how I would play a sound under the button_click event

    So if no one minds showing me the code  that would be awsome =)

    Thanks in advanced!

     

    private void myButtonToPlayMyMusic_Click (object sender, EventArgs ee)

    {

    string action = "C:\whateverYourMusicFileIs.wav";

    System.Diagnostics.Process.Start ( action );

    }

    Tuesday, December 4, 2007 8:03 PM