S
SagaV9
Guest
Hi all,
I am experimenting with a stopwatch app in C# (VS 2013/2015); however, I am not satisfied with the result because the app sometimes behaves in odd ways. I have it working, but the count display stops while the sound plays (roughly 2, 3 seconds). I found that I can use Play() - instead of PlaySync(), but changing this introduces other issues, as described below.
The app is a simple stopwatch that counts up. At defined intervals during the count, it plays a WAV file using the System Media SoundPlayer. For example, given a 10 second interval, it plays the WAV file very 10 seconds.
Sometimes, it stutters. For example, if my WAV file plays a ding, it sounds like d-d-d-d-ding.
In another case, at some intervals, it plays the WAV twice.
I created a simple demo app. I selected a Win Form App, added two labels and a timer component using the tool box. The code is the following:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SampleTimer
{
public partial class Form1 : Form
{
Stopwatch stopWatch = new Stopwatch();
int iInterval = 0;
//File spec for the WAV file.
// WavSource: Sound Effects 1 / Free Wav Files and Sound Clips
string sSoundFile = @"C:\sgvWork\Projects\SampleTimer\chime.wav";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 500;
}
private void timer1_Tick(object sender, EventArgs e)
{
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
//Display count.
label1.Text = String.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds);
//Get number of seconds elapsed.
if (ts.Seconds % 10 == 0 && ts.Seconds > 0)
{
using (System.Media.SoundPlayer player = new System.Media.SoundPlayer(sSoundFile))
{
iInterval++;
player.Play();
//label2.Text = iInterval.ToString();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
stopWatch.Start();
}
}
}
Label2 is just to display other data. After this did not 100%. I did further research and found information about how to implement this functionality using threads and system timers. Apparently, using the boxed Timer control as I did is not recommended. I am (slowly) implementing these techniques, but wanted to ask here for any recommendations,advice or orientation about how to best play sounds while the stopwatch s ticking.
Thank you for your time and assistance. Saga
You can't take the sky from me
Continue reading...
I am experimenting with a stopwatch app in C# (VS 2013/2015); however, I am not satisfied with the result because the app sometimes behaves in odd ways. I have it working, but the count display stops while the sound plays (roughly 2, 3 seconds). I found that I can use Play() - instead of PlaySync(), but changing this introduces other issues, as described below.
The app is a simple stopwatch that counts up. At defined intervals during the count, it plays a WAV file using the System Media SoundPlayer. For example, given a 10 second interval, it plays the WAV file very 10 seconds.
Sometimes, it stutters. For example, if my WAV file plays a ding, it sounds like d-d-d-d-ding.
In another case, at some intervals, it plays the WAV twice.
I created a simple demo app. I selected a Win Form App, added two labels and a timer component using the tool box. The code is the following:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SampleTimer
{
public partial class Form1 : Form
{
Stopwatch stopWatch = new Stopwatch();
int iInterval = 0;
//File spec for the WAV file.
// WavSource: Sound Effects 1 / Free Wav Files and Sound Clips
string sSoundFile = @"C:\sgvWork\Projects\SampleTimer\chime.wav";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 500;
}
private void timer1_Tick(object sender, EventArgs e)
{
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
//Display count.
label1.Text = String.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds);
//Get number of seconds elapsed.
if (ts.Seconds % 10 == 0 && ts.Seconds > 0)
{
using (System.Media.SoundPlayer player = new System.Media.SoundPlayer(sSoundFile))
{
iInterval++;
player.Play();
//label2.Text = iInterval.ToString();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
stopWatch.Start();
}
}
}
Label2 is just to display other data. After this did not 100%. I did further research and found information about how to implement this functionality using threads and system timers. Apparently, using the boxed Timer control as I did is not recommended. I am (slowly) implementing these techniques, but wanted to ask here for any recommendations,advice or orientation about how to best play sounds while the stopwatch s ticking.
Thank you for your time and assistance. Saga
You can't take the sky from me
Continue reading...