steveh44 0 Newbie Poster

Hello,

I'm using the System.Media.SoundPlayer class to play a .WAV file that 'ticks' every 500ms for 1 minute. The WAV file is exactly 60 seconds long. The 'tick sounds' must stay exactly in time with a countdown timer which runs on a background worker thread. The WAV file is loaded in the class constructor like this..

static Sound()
 {
  soundTicker.SoundLocation = (Path.Combine(dirPath, "ticker.wav"));
  soundTicker.LoadAsync();
  //'IsLoadCompleted' property returns 'true' at this point.
 }

static SoundPlayer soundTicker = new SoundPlayer();

public static void PlayTickingSound()<
 {
  // Triggered from a button
  soundTicker.Play();
  //soundTicker.PlayLooping();
 }

All's well so far. The countdown timer and tick sounds are in perfect unison.

As the .WAV file was over 2.5MB I decided to edit down to 1 second (90KB) and use the PlayLooping() method instead.

The trouble is there is a slight glitch the first time the sound is played. This causes the clicks to become out of sync with the timer.

When the timer is stopped and restarted it's back in sync. So I assume the problem must be with the PlayLooping() method.

I found a workaround for this in the constructor:

soundTicker.SoundLocation = (Path.Combine(dirPath, "ticker.wav"));
soundTicker.LoadAsync();
soundTicker.Play();
soundTicker.Stop(); // Stop immediately after starting

Not a very elegant solution but it works. I wonder if anybody could advise me on this.

Many Thanks

SteveH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.