locked
click = sound RRS feed

  • Question

  • hey, complete noob here

    so far i have a form with several buttons. i would like each button to play a different sound loop within the app with the same button starting and stopping the loop. the buttons need to not play the loops at the same time, in other words, only one loop at a time each button starting a loop and stopping the one playing while clicking the current loop stops play.

    sorry, its a lot to ask, any ideas?

    Friday, June 22, 2007 7:46 AM

Answers

  • Okay first of all I recommend you to use SoundPlayer class from System.Media namespace.

    How to play sound:

    // New instance of SoundPlayer class
    SoundPlayer player = new SoundPlayer();

    // Define the location of your soundfile
    player.SoundLocation = "C:\\Sounds\\Soundfile.wav";

    // Load the soundfile
    player.Load();

    // Play the soundfile
    player.Play();


    Button events:


    Now you want to start playing the sound by pressing the button and stop it by pressing the same button again.

    Put following lines outside buttons click event

    // Tells you if the sound is playing or not
    bool isSoundPlaying = false;
    // Soundplayer which we use to play sounds
    SoundPlayer player;


    Put the following code into your buttons click event:

    // If the sound is not playing, start playing it
    if(isSoundPlaying == false) {

        // Maybe we should define here the soundfile we use because it's different for all the buttons

        player = new SoundPlayer();

        // Define the location of your soundfile
        player.SoundLocation = "C:\\Sounds\\Soundfile.wav";

        // Load the soundfile
        player.Load();

        // Play the soundfile
        player.Play();

        // The sound is playing
        isSoundPlaying = true;
    }

    // Otherwise the sound is playing so it's going to be stopped
    else {
        player.Stop();
        isSoundPlaying = false;
    }

    Regards,
    Timo


    Friday, June 22, 2007 8:12 AM
  • This will loop a sound file

     

                My.Computer.Audio.Play(My.Resources.file name, AudioPlayMode.BackgroundLoop)

     

    it will loop untill you try to play another sound from another button

     

    if you use a file from your resources then it must be a wave file

    otherwise you can use your own path instead of my.resources

     

     

    I haven't used soundplayer, i will have take a look and see if that is any better.  It may be.

    is that c# code?

     

     

     

    Friday, June 22, 2007 1:03 PM
  • I found another project at the above site that uses soundplayer

    download the playwavefileex-beta2

     

    to use the soundplayer in vb you have to declare it with something like this

     

    Dim snd As Media.SoundPlayer = New Media.SoundPlayer

    Dim theFile As String = Nothing

     

    This project works better than the directx in some areas

    i couln't figure out how to keep the sound playing when the form loses focus in the directx one.

    Friday, June 22, 2007 9:10 PM
  • Not sure about that one.

     

    do you have the correct file name used instead of just the words file name?  Probably a dumb question but just making sure.

     

    Have you tried removing the file and then adding it back into your resources? 

     

    Or maybe try renaming the file.

    Tuesday, June 26, 2007 11:57 PM

All replies

  • Okay first of all I recommend you to use SoundPlayer class from System.Media namespace.

    How to play sound:

    // New instance of SoundPlayer class
    SoundPlayer player = new SoundPlayer();

    // Define the location of your soundfile
    player.SoundLocation = "C:\\Sounds\\Soundfile.wav";

    // Load the soundfile
    player.Load();

    // Play the soundfile
    player.Play();


    Button events:


    Now you want to start playing the sound by pressing the button and stop it by pressing the same button again.

    Put following lines outside buttons click event

    // Tells you if the sound is playing or not
    bool isSoundPlaying = false;
    // Soundplayer which we use to play sounds
    SoundPlayer player;


    Put the following code into your buttons click event:

    // If the sound is not playing, start playing it
    if(isSoundPlaying == false) {

        // Maybe we should define here the soundfile we use because it's different for all the buttons

        player = new SoundPlayer();

        // Define the location of your soundfile
        player.SoundLocation = "C:\\Sounds\\Soundfile.wav";

        // Load the soundfile
        player.Load();

        // Play the soundfile
        player.Play();

        // The sound is playing
        isSoundPlaying = true;
    }

    // Otherwise the sound is playing so it's going to be stopped
    else {
        player.Stop();
        isSoundPlaying = false;
    }

    Regards,
    Timo


    Friday, June 22, 2007 8:12 AM
  • This will loop a sound file

     

                My.Computer.Audio.Play(My.Resources.file name, AudioPlayMode.BackgroundLoop)

     

    it will loop untill you try to play another sound from another button

     

    if you use a file from your resources then it must be a wave file

    otherwise you can use your own path instead of my.resources

     

     

    I haven't used soundplayer, i will have take a look and see if that is any better.  It may be.

    is that c# code?

     

     

     

    Friday, June 22, 2007 1:03 PM
  • looks like it is, (annoying how ppl post C# and VB 6 code on these topics...)

    but the commands should work the same, just take out teh curly brackets and stuff not belonging to vb..

    Friday, June 22, 2007 4:34 PM
  • I came across this site and found a sound project that has some increased functionality

     

    http://www.vbcodesource.com/exampleNet.html

     

    Look for the project with the title

    DirectX-DirectSound 9.0 Example

    Friday, June 22, 2007 8:32 PM
  • I found another project at the above site that uses soundplayer

    download the playwavefileex-beta2

     

    to use the soundplayer in vb you have to declare it with something like this

     

    Dim snd As Media.SoundPlayer = New Media.SoundPlayer

    Dim theFile As String = Nothing

     

    This project works better than the directx in some areas

    i couln't figure out how to keep the sound playing when the form loses focus in the directx one.

    Friday, June 22, 2007 9:10 PM
  • thanks to everyone and js06, i think i can manage it now
    Saturday, June 23, 2007 12:51 PM
  •  MazterzDemon wrote:

    looks like it is, (annoying how ppl post C# and VB 6 code on these topics...)

    but the commands should work the same, just take out teh curly brackets and stuff not belonging to vb..



    I'm sorry, didn't notice that this is VB.net forum...
    Saturday, June 23, 2007 12:56 PM
  • js06, it's easy to use SoundPlayer class but I'm not sure if it's in VB dotnet. The problem is that it can only play wav files so I don't recommend using it if you want to play MP3s or even WMAs.

    Regards,
    Timo
    Saturday, June 23, 2007 1:01 PM
  • this is my first program so this is all i could make work:

     

    My.Computer.Audio.Play(My.Resources.file name, AudioPlayMode.BackgroundLoop)

     

    if i run it without debugging it works great, but debugging gives me an error:

     

    System.BadImageFormatException was unhandled
      Message="Corrupt .resources file. Got an unexpected EndOfStreamException while trying to read the ResourceReader header."
      Source="mscorlib"
     

    any suggestions?

    Tuesday, June 26, 2007 10:59 PM
  • Not sure about that one.

     

    do you have the correct file name used instead of just the words file name?  Probably a dumb question but just making sure.

     

    Have you tried removing the file and then adding it back into your resources? 

     

    Or maybe try renaming the file.

    Tuesday, June 26, 2007 11:57 PM
  • yes. for some reason removing, renaming, and re-adding the sound files did the trick!

    Wednesday, June 27, 2007 1:42 AM