1

So I am using a win form application using the latest framework for c#.

Here I have a button that plays a sound (asterisk) and then after 5 secs or 20 secs(I let the user pick) I want it to play a different sound (beep in this case) But when it calls the code to play beep the sound I hear is Asterisk.

finally at the end I want another sound to signal that it has finished. I have chosen (Exclamation) but again I hear asterisk.

Does anyone know why I would only hear the one sound?

P.S. I have tried switching the start sound to beep and the "next" sound to asterisk. I clean and rebuild but then the only sound I hear is a beep.

Here is the code behind for the button click

 private void btnStart_Click(object sender, EventArgs e)
            {
                BusinessLayer.ToneApp.PlaySound play = new BusinessLayer.ToneApp.PlaySound();

                _countDown = play.IsValid(txtTime.Text);
                numreps = play.IsValid(txtReps.Text);
                numsets = play.IsValid(txtSets.Text);

                lblFinished.Text = "";


                play.PlaySoundStart();
                _timer = new System.Windows.Forms.Timer();
                _timer.Enabled = true;
                _timer.Tick += new EventHandler(timer1_Tick);
                _timer.Interval = 1000;
                _timer.Start();

            }

in the timer1_tick event I have

private void timer1_Tick(object sender, EventArgs e)
        {
            BusinessLayer.ToneApp.PlaySound play = new BusinessLayer.ToneApp.PlaySound();

            _countDown--;
            if (_countDown < 1)
            {
//my code
                play.PlaySoundNext();
}
}

and finally here is my playsound

    public class PlaySound
    {
        public void PlaySoundStart()
        {
            System.Media.SystemSounds.Asterisk.Play();

        }

        public void PlaySoundNext()
        {
            System.Media.SystemSounds.Beep.Play();

        }

        public void PlaySoundStop()
        {
            System.Media.SystemSounds.Exclamation.Play();

        }



        public int IsValid(string textToInt)
        {
            int.TryParse(textToInt, out int timeInSeconds);

            return timeInSeconds;
        }

    }
jtslugmaster08
  • 182
  • 1
  • 16
  • `timer.Enabled = true;` and `timer.Start();` do the same thing, btw. Why recreate the timer every time the button is pressed? What value do you get from `_countDown = play.IsValid(txtTime.Text);`? – LarsTech Jan 21 '19 at 17:30
  • have you checked to see if PlaySoundNext() method ever fires? My question is if the line: System.Media.SystemSounds.Beep.Play(); is still playing the asterisk sound? – Icculus018 Jan 21 '19 at 17:33
  • @kristech yes the playsoundNext() line is hit I had a debugger and after I hit f5 over it the asterisk sound played not beed. which is really weird. – jtslugmaster08 Jan 21 '19 at 17:39
  • @LarsTech Didn't know they did the same thing so thanks :) Also Now I will disable the button until the countdown is finished. I get the value I (user) typed in. Whether I get input from the user or just hard code it the result is the same. And outside the scope of this question. Everything is working regarding the other parts of the code the only issue I am having is one sound being played. – jtslugmaster08 Jan 21 '19 at 17:39
  • 2
    That is simply because on any recent Windows version these named sounds all have the same sound file attached. Use Control Panel > Sounds > Sounds tab to test and configure. – Hans Passant Jan 22 '19 at 01:06
  • @HansPassant wow that was it. So how would I access the sound files or attach the correct sound file because I cant assume the correct sound files are attached. – jtslugmaster08 Jan 22 '19 at 19:27

1 Answers1

3

You should not expect SystemSounds to have any particular sound, because the user can change them. Call Beep or Asterisk to signal something or bring more attention to something. I think Hand is meant to signal an event where something stops unexpectedly or in an unusual way. There is a broader answer regarding this, which also points out the possibility of adding your own system sounds into Windows.

It seems that on latest versions of Windows, the default sounds for Asterisk, Beep and Exclamation are all the same sound. I checked on Windows 10, version 1909. Hand is different and Question has no sound at all. I guess it was made this way, because the first three were used interchangeably due to lack of clear distinction of when it is appropriate to use each of them. And the Question sound probably got removed, because prompts with questions are annoying enough even without the sound.

Custom sounds

You can embed your own sounds into the app. For that you can check out System.Media.SoundPlayer. Embedded resources can be loaded using Assembly.GetManifestResourceStream. Alternatively the sound file can be added to a resources file and accessed though an auto-generated class.

Andrius R.
  • 159
  • 1
  • 7