1

tl;dr version: How can I start playing a sound in the MouseDown event of a WinForms button and stop it from playing in the same button's MouseUp event?

Intermediate C# dev here and I've been trying to write a simple Simon clone.

I'm currently stuck trying to make it play the sound for the colored button only while the user is clicking the button. (I use "button" and "tile" interchangeably. Both refer to the colored button the user will press on the form)

I originally tried this:

public partial class frmMain : Form
{
    private SoundPlayer soundPlayer;

    private void btnGreenTile_MouseDown(object sender, MouseEventArgs e)
    {
        soundPlayer = new SoundPlayer(Properties.Resources.greenTileSound)
        soundPlayer.Play();
    }

    private void btnGreenTile_MouseUp(object sender, MouseEventArgs e)
    {
        soundPlayer.Stop();
    }
}

But this didn't stop the sound because the MouseUp event didn't fire due to MouseDown not being finished (still playing the sound which is like 5 seconds long in case someone holds the button for longer than a simple click). As mentioned by Luis Tellez in the comments, SoundPlayer plays the sound on it a new thread...so I have no idea why this code doesn't work now.

So I looked into multithreading and tried this:

public partial class frmMain : Form
{
    private Thread soundThread = new Thread(new ParameterizedThreadStart(PlaySound));

    // Create stream objects for each sound (needed to allow SoundPlayer to use Resources)
    private Stream greenTileSound = Properties.Resources.greenTilePress;
    private Stream redTileSound = Properties.Resources.redTilePress;
    private Stream yellowTileSound = Properties.Resources.yellowTilePress;
    private Stream blueTileSound = Properties.Resources.blueTilePress;

    public frmMain()
    {
        InitializeComponent();
    }

    private void btnGreenTile_MouseDown(object sender, MouseEventArgs e)
    {
        soundThread.Start(greenTileSound); 
    }

    private void btnGreenTile_MouseUp(object sender, MouseEventArgs e)
    {
        soundThread.Abort();
    }

    // Have to use object as parameter because ParamterizedThreadStart() only takes object arguments
    private static void PlaySound(object soundToPlay)
    {
        SoundPlayer soundPlayer = new SoundPlayer((Stream)soundToPlay);
        soundPlayer.Play();
    }
} 

With the above code, it does not stop playing the sound on MouseUp, and even better it throws a ThreadStateException with the message "Thread is running or terminated; it cannot restart."

As you can probably tell, I only just learned about multithreading while trying to write this code. I have to use ParameterizedThreadStart because the method it calls when the thread starts, PlaySound(), needs to pass a parameter to soundPlayer with the resource corresponding to the colored button that was pressed by the player (a .wav file).

I then thought maybe I should try using soundThread.Suspend() instead of soundThread.Abort() but Suspend is deprecated...

Can anyone point me in the right direction to get the sound to stop on MouseUp? Do I need to work with multithreading? I think my problem just comes down to logic but I am fully stuck. Thank you for any and all help! :)

As a side note, I'm kind of surprised that this question or something similiar has not been asked yet (at least I couldn't find it with google searches or stackExchange searches).

3
  • I've tried to recreate your problem (first, non-threaded approach), but did it by pointing SoundPlayer to exact file location rather than using a stream and it works just fine, so try to look into how streams work, the problem might be there.
    – S_F
    Jan 4, 2015 at 9:37
  • Scratch that, it even works with streams. What's the code behind Properties.Resources.greenTilePress?
    – S_F
    Jan 4, 2015 at 9:48
  • Properties.Resources.greenTilePress is a resource (.wav file) I added the the visual studio project so it would be included with the exe. After your comment I tried the non-threaded approach again and it worked (I literally copied and pasted the code from here). I have no idea why it didn't work before. Thanks for your help! Jan 4, 2015 at 9:57

1 Answer 1

1

You can see in the documentation that the play() method run in other thread, you can do something else after it and it should run, so your problem with the first approach is something different from what you were thinking.

The Play method plays the sound using a new thread. If you call Play before the .wav file has been loaded into memory, the .wav file will be loaded before playback starts. You can use the LoadAsync or Load method to load the .wav file to memory in advance. After a .wav file is successfully loaded from a Stream or URL, future calls to playback methods for the SoundPlayer will not need to reload the .wav file until the path for the sound changes. If the .wav file has not been specified or it fails to load, the Play method will play the default beep sound.

http://msdn.microsoft.com/en-us/library/system.media.soundplayer.play%28v=vs.110%29.aspx

2
  • 1
    I cant see a reason in the code you posted, as far as I understand it should work, if you have this on GIT, I can clone it and test it out. Jan 4, 2015 at 9:57
  • 1
    Yes the first code does work. I have no idea why it didn't work for me before but I obviously had screwed something up. Thank you very much for your time and help! Jan 5, 2015 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.