Answered Stop SoundPlayer in 'Alarm' program.

Joined
Jan 31, 2021
Messages
10
Programming Experience
Beginner
Hi there UAll,

New in using CSharp and learning step by step - also following some creations from YouTube.
So Here I am to put my question to you all.

First, here's my program - Clock Alarm:
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace ClockAlarm
{
    public partial class Form1 : Form
    {
        System.Timers.Timer timer;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer = new System.Timers.Timer();
            timer.Interval = 1000;
            timer.Elapsed += Timer_Elapsed;

        }

        delegate void UpdateLabel(Label lbl, string value);
        void UpdateDataLabel(Label lbl, string value)
        {
            lbl.Text = value;
        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            DateTime currentTime = DateTime.Now;
            DateTime userTime = dateTimePicker.Value;
            if (currentTime.Hour == userTime.Hour && currentTime.Minute == userTime.Minute && currentTime.Second == userTime.Second)
            {
                timer.Stop();
                try
                {
                    UpdateLabel upd = UpdateDataLabel;
                    if (lblStatus.InvokeRequired)
                        Invoke(upd, lblStatus, "STOP");
                    SoundPlayer player = new SoundPlayer();
                    player.SoundLocation = @"C:\Programmeren in C Sharp\Projecten\ClockAlarm\Electric Six - Danger! High Voltage.wav";
                    player.PlayLooping();
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void bttnStart_Click(object sender, EventArgs e)
        {
            timer.Start();
            lblStatus.Text = "Programma is lopende ...";
        }

        private void bttnStop_Click(object sender, EventArgs e)
        {
            timer.Stop();
            lblStatus.Text = "Programma gestopt door de gebruiker.";
        }
    }
}
When I start the program, no problem.
When the alarm goes on, I click stop, but the music remains playing.
I can put in my last lines from bttnStop_Click:
player.Stop();
However, when I try his, the program automatic tries to put SoundPlayer insted of player.
My question,
How can I stop also the music playing when I click on 'STOP'.

Kind regards and untill next time.

Lets Code!

Wim.
 
Last edited by a moderator:
Move the player declaration outside the method, so both methods can access it.
 
Time for our OP to learn about C# basics like scopes because they obviously skipped over it and straight to WinForms programming.
 
Hi there,
I've adjusted this and it works properly.
Thanks.

Now another problem.
I want to set this little program on the computers of my kids. So in the debug file I can find the executabe file. If I copy this file on their computer it works, yet the WAV file that I also copy/paste in the wright map in their's gives problems. The link in the program to load the wav file is in the same folders.

can i get some tips

Thx in front!
 
Show us your updated code. The reason I ask is because in your OP, on line 50 you hard coded the path to your WAV file. If that exact path does not exist on your kids machine does not exist, or is your kids don't have permissions to that path, then the WAV file cannot be loaded.

In general, you should embed resources like images and music as resources in your assembly instead of as loose files. That way you know that when you deploy your app, it is completely self-sufficient. Additionally it raises the bar for anyone who wants to replace your resource files with some other files. Imagine your kids replacing renaming a wave file with Rick Astley's "Never Gonna Give You Up" to "Electric Six - Danger! High Voltage.wav".
 
Unfortunately your rewritten code didn't post.
 
ClockAlarm:
namespace ClockAlarm
{
    public partial class Form1 : Form
    {
        System.Timers.Timer timer;
        SoundPlayer player = new SoundPlayer();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer = new System.Timers.Timer
            {
                Interval = 1000
            };
            timer.Elapsed += Timer_Elapsed;
            player.SoundLocation = @"C:\ClockAlarmFile\Alarm.wav";
            player.Load();

        }


        delegate void UpdateLabel(Label lbl, string value);
        void UpdateDataLabel(Label lbl, string value)
        {
            
            lbl.Text = value;
        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //"Current time" is niet "de tijd nu"?!
            DateTime currentTime = DateTime.Now;
            DateTime userTime = dateTimePicker.Value;
            if (currentTime.Hour == userTime.Hour && currentTime.Minute == userTime.Minute && currentTime.Second == userTime.Second)
            {
                timer.Stop();
                try
                {
                    UpdateLabel upd = UpdateDataLabel;
                    if (lblStatus.InvokeRequired)
                        Invoke(upd, lblStatus, "Timer is gestopt.");
                    player.Play();
                    player.PlayLooping();

                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void bttnStart_Click(object sender, EventArgs e)
        {
            timer.Start();
            lblStatus.Text = "Timer Started.";
        }

        private void bttnStop_Click(object sender, EventArgs e)
        {
            timer.Stop();
            lblStatus.Text = "Timer Stoped.";
            player.Stop();
        }
    }
}
 
So does the file "C:\ClockAlarmFile\Alarm.wav" exist on your kids' machines? Do their accounts have permission to read the file?
 
Back
Top Bottom