-1

I've been trying to get this button to do what I want for a little now. I want it to play a sound when I press it, which is what I managed to do. However, its like the application freezes every time you press the button, giving the sound all of its attention, etc. So basically my goal is to make the button play a sound without making the UI have to stop and allow it to play, before moving on. I also would like to know if there is a way to make a button play sound when pressed, but when pressed again the current sound is stopped and plays again, to prevent it from playing "X" amount of clicks you clicked the button, etc.

Here is my code:

    public static void ButtonSound()
    {
        SoundPlayer _sound = new SoundPlayer();
        try
        {
            _sound.Stop();
            _sound.SoundLocation = path + "ButtonTap.wav";
            _sound.Load();
            _sound.PlaySync();
        }
        catch
        {

        }
        finally
        {
            _sound.Dispose();
        }
    }

And Button code:

    private void Button()
    {
        SoundPlayers.ButtonSound();
    }

Note, I have my SoundPlayer in another class, and I am using DelegateCommands to reference my Button() method, etc. I am also using .wav files. Also, is there a more efficient way to achieve that task I am trying to accomplish? If you need anything else, just ask.

Thanks!

Hypergyzed
  • 45
  • 1
  • 2
  • 10

1 Answers1

0

Your UI is freezing because that is what you are asking for when you call _sound.PlaySync().

Looking at the SoundPlayer documentation, you could use the Play() method:

Plays the .wav file using a new thread, and loads the .wav file first if it has not been loaded.

Using this method should also solve the problem of playing the sound repeatedly, since you will no longer be queuing your calls.

Also, what you did there with Try-Finally-Dispose can be simplified with using, so your code would look like this:

public static void ButtonSound()
{
    using (var _sound = new SoundPlayer())
    {
        _sound.SoundLocation = path + "ButtonTap.wav";
        _sound.Play();
    }
}

Note that your _sound.Stop() doesn't make sense, since you are calling it on a new object, but just calling Play() on a new object will make the old sound stop and then play the new one.

Also, the doc says that Play() loads the file if it has not been loaded, that is why I skipped the _sound.Load() call.

Gabriel Prá
  • 1,377
  • 10
  • 21
  • If *"[...] just disposing the SoundPlayer will make it stop."* is true, then you can't really put the call of the Play method in a using block like you did, or? (I know that you wanted to show how to utilize using blocks instead of try-finally-dispose, but in the context of your answer it seems to be a little flawed...) –  May 27 '17 at 18:43
  • I tested this and it does work, so I'm wrong when I say disposing it stops the sound. It seems that what makes the sound stop is calling `Play()` again. Thanks! – Gabriel Prá May 27 '17 at 18:51
  • Okay, and what about the fact that if I were to spam the button, it plays the exact same sound for x amount of clicks? – Hypergyzed May 27 '17 at 20:57
  • I tested it here and it does not do that with the asynchronous play. That is what I meant with _"just calling Play() on a new object will make the old sound stop and then play the new one."_ – Gabriel Prá May 28 '17 at 00:00