Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have a windows form application that allows me to see count down. I would like to play a warning sound in a specific time. For example at 11:06 am.

C#
var curTime = DateTime.Now.TimeOfDay;
var soundTime= new TimeSpan(11, 06, 0);

......

 if(curTime==soundTime)
{
   SoundPlayer simpleSound = new SoundPlayer(@"c:\Windows\Media\Windows Logon Sound.wav");
   simpleSound.Play();
}


Basically when the count down clock reaches the specific time, I wanted to play that sound file however it doesn't work.
Posted
Updated 13-May-14 12:23pm
v3
Comments
CHill60 13-May-14 12:51pm    
"Doesn't work" ... doesn't go into your code at all or it does get there but the sound doesn't play?
fatihkaratay 13-May-14 12:58pm    
It does get there however the sound file doesn't play. I create a separate file for testing the sound file and it worked. However when the time is arrived, it doesn't play the sound.
CHill60 13-May-14 14:36pm    
It is probably because curTime will also have milliseconds, so your test if(curTime==soundTime) would be unlikely to be true (Chances of hitting the exact 0 milliseconds is slim!). Try if(curTime>=soundTime) instead (and remember to reset the timer or loop or whatever you are using to count down)
fatihkaratay 13-May-14 15:37pm    
That works. I should have thought before I post. Thank you!

OP has confirmed that my suggestion in a comment works

Quote:
It is probably because curTime will also have milliseconds, so your test if(curTime==soundTime) would be unlikely to be true (Chances of hitting the exact 0 milliseconds is slim!). Try if(curTime>=soundTime) instead (and remember to reset the timer or loop or whatever you are using to count down)

So the OPs code becomes
C#
var curTime = DateTime.Now.TimeOfDay;
var soundTime= new TimeSpan(11, 06, 0);


......

 if(curTime >= soundTime)
            {
                SoundPlayer simpleSound = new SoundPlayer(@"c:\Windows\Media\Windows Logon Sound.wav");
                simpleSound.Play();
            }
 
Share this answer
 
Comments
ZurdoDev 13-May-14 21:44pm    
+5. You did ask the OP if the code got to it and they claimed it did.

Nope. It wasn't. So much can be solved on your own if you just debug code. :)
This works for me

C#
var soundTime= new TimeSpan(11, 06, 0);
DateTime start = new DateTime(2009, 12, 9, 10, 0, 0); //10 o'clock
DateTime end = new DateTime(2015, 12, 10, 12, 0, 0); //12 o'clock
DateTime curTime = DateTime.Now;
if ((curTime > start) && (curTime < end))

 
 //if(curTime==soundTime)
            {
                System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"c:\Windows\Media\Windows Logon Sound.wav");
                player.Play();
            }
 
Share this answer
 
I am assuming than your code is running in a timer (actually I am sure)

C#
var soundTime = new TimeSpan(11, 06, 0);

var curDate = DateTime.Now;
if (soundTime.Equals(new TimeSpan(curDate.Hour, curDate.Minute, curDate.Second)))
{
   SoundPlayer simpleSound = new SoundPlayer(@"c:\Windows\Media\Windows Logon Sound.wav");
   simpleSound.Play();
}


If your code block is in timer tick event, if(curTime >= soundTime) will fire continuously and will fail.
 
Share this answer
 
v2
Comments
CHill60 13-May-14 16:11pm    
See my (quoted) comment "and remember to reset the timer or loop or whatever you are using to count down". I actually used my test code in a Timer tick event and it worked ok - as long as I stopped the timer or changed the soundTime (to past or far future) [Edit - "past" d'oh. I didn't try that - for obvious reasons!]
Emre Ataseven 13-May-14 16:29pm    
Right, I did not see your comment :)
CHill60 13-May-14 16:33pm    
That's ok ... I'm clearly not seeing straight myself at the moment :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900