I have an application that plays short (2-5 seconds) sounds from resources at
specific times. About 10% of the time the wav file will start playing fine
and at some undetermined point during the playback it will play loud tones
for the duration of the sound files normal time. I cannot figure out what
actually causes it. I can click a different button and have another sound
play fine out of the resource while the first sound is malfunctioning. It
appears that the malfunctions are completely random. It only appears to
occur when i play the sound from a resource, as i seem to be able to play
sounds fine when i point them at the file on the harddrive instead of passing
in the stream from the resource.
The code is very simple. I have an overall setting that turns sounds on and
off. Then a setting that allows the user to change the applause sound. If
the path is empty or doesn't point to a valid sound file it will play the
default from a resource
public static void PlayApplause()
{
if (!PlaySound(Settings.Default.ApplauseSoundPath))
PlaySound(Resources.applausesmall_Sound);
}
public static bool PlaySound(Stream toPlay)
{
bool rVal = false;
try
{
if (Settings.Default.PlaySounds)
{
SoundPlayer sp = new SoundPlayer(toPlay);
sp.Load();
sp.Play();
rVal = true;
}
}
catch { rVal = false; }
return rVal;
}
public static bool PlaySound(string Path)
{
bool rVal = false;
try
{
if (Settings.Default.PlaySounds &&
!string.IsNullOrEmpty(Path) && File.Exists(Path))
{
SoundPlayer sp = new SoundPlayer(Path);
sp.Load();
sp.Play();
rVal = true;
}
}
catch
{
rVal = false;
}
return rVal;
}