JonP
JonP

Reputation: 23

Need to play a sound alert at a certain time in a stopwatch application in C#

I have written a simple stopwatch application that needs a sound alert played at certain intervals. The sound alert needs to play at the 15 minute mark, the 30 minute mark and the 1 hour mark. I've got the sound file set up to play when the application starts but I have hit a wall on how to get it to play at those intervals. The code below is the stopwatch portion of the code.
Also note that I have not initialized the sound in this version of the code. Any help would be greatly appreciated.

using System;
using System.Diagnostics;
using System.IO;
using System.Media;
using System.Security.Cryptography.X509Certificates;
using System.Windows.Forms;

namespace CiscoSw
{
    public partial class SwForm : Form
    {

        private readonly Stopwatch _sw = new Stopwatch();

        internal SwForm()
        {
            InitializeComponent();
            startBtn.Text = @"Start";
            UpdateDisplay();
        }

        private void SwForm_Load(object sender, EventArgs e)
        {
            currentTimeUtc.Start();
        }

        private void startBtn_Click(object sender, EventArgs e)
        {
            if (!_sw.IsRunning)
            {
                _sw.Start();
                stopwatchTimer.Start();
                startBtn.Text = @"Stop";
                UpdateDisplay();
            }
            else
            {
                _sw.Stop();
                stopwatchTimer.Stop();
                startBtn.Text = @"Start";
            }
        }

        private void resetBtn_Click(object sender, EventArgs e)
        {
            if (_sw.IsRunning)
            {
                _sw.Restart();
            }
            else
            {
                _sw.Reset();
            }
            UpdateDisplay();
        }

        internal void stopwatchTimer_Tick(object sender, EventArgs e)
        {
            UpdateDisplay();
        }

        private void UpdateDisplay()
        {
           stopwatchTimeLabel.Text =
                $"{_sw.Elapsed.Hours.ToString("00")}:{_sw.Elapsed.Minutes.ToString("00")}:{_sw.Elapsed.Seconds.ToString("00")}";
        }

        private void currentTimeUtc_Tick(object sender, EventArgs e)
        {
            var now = DateTime.UtcNow;
            displayCurrentTime.Text = $"{now:M/d/yyyy   HHmm Zulu}";
        }
    }

}

This code is for the sound/s to play. Right now I just have one sound file in the resources and once I get that working properly I will add the others. This code sits in a separate file in the project.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using static System.DateTime;

namespace CiscoSw
{
    public class TimerSound
    {
        public virtual void TimerSoundPlay() 
        {
            var fifteenminSound = new SoundPlayer(Properties.Resources.ResourceManager.GetObject("fifteenminutes") as Stream);
            fifteenminSound.Play(); 
        }

    }
}

Upvotes: 0

Views: 492

Answers (2)

CathalMF
CathalMF

Reputation: 10055

This should work. I havent tested the code. Im using System.Windows.Forms.Timer

Edit: I just tested this with a shorter time interval and it works perfectly. I also moved the MakeSound call to the end of the event so that it doesnt hold up the resetting of the timers.

    Timer Timer15 = new Timer();
    Timer Timer30 = new Timer();
    Timer Timer60 = new Timer();

    public void SetupTimers()
    {            
        Timer15.Interval = 15 * 60 * 1000;
        Timer30.Interval = 30 * 60 * 1000;
        Timer60.Interval = 60 * 60 * 1000;

        Timer15.Tick += Timer_Tick;
        Timer30.Tick += Timer_Tick;
        Timer60.Tick += Timer_Tick;

        Timer15.Enabled = true;
        Timer30.Enabled = true; 
        Timer60.Enabled = true;
    }

    void Timer_Tick(object sender, EventArgs e)
    {
        Timer timer = sender as Timer;
        // Unsubscribe from the current event. Prevents multuple subscriptions. 
        timer.Tick -= Timer_Tick;   
        // If 60 minutes then reset all timers. 
        if (timer.Interval == 60 * 60 * 1000)
        {
            SetupTimers();
        } 

        //Call sound method here. 
        MakeSound();            
    }

Upvotes: 1

Kayani
Kayani

Reputation: 972

One option is to use Timer in parallel with the stop watch and raise an event on the timer when 15 mins or whatever time interval passes. make the sound on that event. However you cannot attach events directly with the stopwatch

Upvotes: 1

Related Questions