8 Replies - 441 Views - Last Post: 13 November 2019 - 12:33 AM Rate Topic: -----

#1 dongtrien   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 17-June 15

Is the command to check if the bell is running or stopped ?

Posted 08 November 2019 - 01:14 AM

I have the following C # statement:
...
using (SoundPlayer player = new SoundPlayer(Properties.Resources.ring))
{
player.Play();

  //What should I write here? 
  while (If the bell is running, wait here)
  {
      if (player.Stop()==true)
      {
          this.progressBarControl1.Visible = false; //false = không cho phép hiện
      }
  }
}
... 


I want to check if the ringer is running or stopping, if it is running then wait until it stops before running this.progressBarControl1.Visible = false.

Is This A Good Question/Topic? 0
  • +

Replies To: Is the command to check if the bell is running or stopped ?

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7132
  • View blog
  • Posts: 24,230
  • Joined: 05-May 12

Re: Is the command to check if the bell is running or stopped ?

Posted 08 November 2019 - 06:44 AM

Unfortunately, you can't because SoundPlayer.Play() just calls the Windows API PlaySound() in async mode. See source code here.

You could try calling SoundPlayer.PlaySync() instead which calls the same API in synchronous mode so that the method call doesn't return until the sound is done playing.
Was This Post Helpful? 0
  • +
  • -

#3 dongtrien   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 17-June 15

Re: Is the command to check if the bell is running or stopped ?

Posted 11 November 2019 - 03:19 AM

method "PlaySync" does not check the status "ON / OFF" when the bell rings, you see my code.
public Form1()
        {
            InitializeComponent();
            lblStatusPlaySound.ForeColor = Color.Violet;
            lblStatusPlaySound.Text = "Status: OFF/ON";
        }
        private void btnPlaySound_Click(object sender, EventArgs e)
        {
            lblStatusPlaySound.ForeColor = Color.Green;
            lblStatusPlaySound.Text = "Status: ON";

            using (SoundPlayer player = new SoundPlayer(Properties.Resources.ring))
            {
                player.PlaySync();
            }

            lblStatusPlaySound.ForeColor = Color.Red;
            lblStatusPlaySound.Text = "Status: OFF";
        }


Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7132
  • View blog
  • Posts: 24,230
  • Joined: 05-May 12

Re: Is the command to check if the bell is running or stopped ?

Posted 11 November 2019 - 09:21 AM

Where do you check the status in that code you presented?

If you are talking about not seeing the label changing from "ON/OFF", then to "ON", and then finally to "OFF", but only see the label go from "ON/OFF" to "OFF", that is because you are not yielding control back to the UI thread for it to paint "ON", play the sound, and then yield control to paint the "OFF". And before you listen to the VB programmers, the correct solution is not to call Application.DoEvents().
Was This Post Helpful? 0
  • +
  • -

#5 dongtrien   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 17-June 15

Re: Is the command to check if the bell is running or stopped ?

Posted 12 November 2019 - 02:02 AM

I agree with my friend that I have not yet called Application.DoEvents(), the SoundPlayer.PlaySync() function allows you to run sequentially (that is, to finish running the next command) but the Windows API PlaySound() does not do it.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7132
  • View blog
  • Posts: 24,230
  • Joined: 05-May 12

Re: Is the command to check if the bell is running or stopped ?

Posted 12 November 2019 - 11:30 AM

Are you sure? Did you read the documentation for PlaySound() closely? It's default behavior is synchronous playback:

Quote

:
SND_ASYNC
The sound is played asynchronously and PlaySound returns immediately after beginning the sound. To terminate an asynchronously played waveform sound, call PlaySound with pszSound set to NULL.
:
SND_SYNC
The sound is played synchronously, and PlaySound returns after the sound event completes. This is the default behavior.
:

Was This Post Helpful? 0
  • +
  • -

#7 dongtrien   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 17-June 15

Re: Is the command to check if the bell is running or stopped ?

Posted 12 November 2019 - 07:09 PM

Hi Skydiver!
You misunderstood my question. I said PlaySound() can't wait for the bell to finish before executing the next command, I troubleshoot I see Debug.Print("Status: PLAY") and Debug.Print("Status: STOP") appear at the same time when the bell is ringing, see my code

//In Form1
private void btnPlaySound_Click(object sender, EventArgs e)
    {            
       lblStatusPlaySound.ForeColor = Color.Green;
       lblStatusPlaySound.Text = "Status: PLAY";            
       Application.DoEvents();         
       Debug.Print("Status: PLAY");                  
      
       Play.PlaySound(@"..\ring.wav");//my path file name ring 
            
       lblStatusPlaySound.ForeColor = Color.Red;
       lblStatusPlaySound.Text = "Status: STOP";            
       Application.DoEvents();
       Debug.Print("Status: STOP");                     
    }


//In clsSoundPlayerStatus.cls
using System.Runtime.InteropServices;

namespace SoundPlayerStatus
{    
    internal static class NativeMethods
    {
        [DllImport("winmm.dll", EntryPoint = "PlaySound", SetLastError = true, CharSet = CharSet.Unicode, ThrowOnUnmappableChar = true)]
        public static extern bool PlaySound(
            string szSound,
            System.IntPtr hMod,
            PlaySoundFlags flags);

        [System.Flags]
        public enum PlaySoundFlags : int
        {
            SND_SYNC = 0x0000,/* play synchronously (default) */
            SND_ASYNC = 0x0001, /* play asynchronously */
            SND_NODEFAULT = 0x0002, /* silence (!default) if sound not found */
            SND_MEMORY = 0x0004, /* pszSound points to a memory file */
            SND_LOOP = 0x0008, /* loop the sound until next sndPlaySound */
            SND_NOSTOP = 0x0010, /* don't stop any currently playing sound */
            SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
            SND_ALIAS = 0x00010000,/* name is a registry alias */
            SND_ALIAS_ID = 0x00110000, /* alias is a pre d ID */
            SND_FILENAME = 0x00020000, /* name is file name */
            SND_RESOURCE = 0x00040004, /* name is resource name or atom */
            SND_PURGE = 0x0040,  /* purge non-static events for task */
            SND_APPLICATION = 0x0080, /* look for application specific association */
            SND_SENTRY = 0x00080000, /* Generate a SoundSentry event with this sound */
            SND_RING = 0x00100000, /* Treat this as a "ring" from a communications app - don't duck me */
            SND_SYSTEM = 0x00200000 /* Treat this as a system sound */
        }
    }
    public static class Play
    {
        public static void PlaySound(string PathFile)
        {
            NativeMethods.PlaySound(PathFile, new System.IntPtr(), NativeMethods.PlaySoundFlags.SND_ASYNC | NativeMethods.PlaySoundFlags.SND_SYSTEM);
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7132
  • View blog
  • Posts: 24,230
  • Joined: 05-May 12

Re: Is the command to check if the bell is running or stopped ?

Posted 12 November 2019 - 08:41 PM

That is because you are passing NativeMethods.PlaySoundFlags.SND_ASYNC. (See line 56 of your code.) Did you not read the documentation, as well as the text I quoted above. When you pass in SND_ASYNC.

Quote

SND_ASYNC
The sound is played asynchronously and PlaySound returns immediately after beginning the sound.


If you want to wait for the sound to finish playing, don't use that flag, or use SND_SYNC.

Also, it looks like you didn't take my advice. I told you that use Application.DoEvents() is the wrong thing to do, yet there you are using it not only once, but twice.
Was This Post Helpful? 0
  • +
  • -

#9 dongtrien   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 17-June 15

Re: Is the command to check if the bell is running or stopped ?

Posted 13 November 2019 - 12:33 AM

I'm sorry friend Skydiver. I am wrong, do not read the document carefully, thank you very much
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1