Stopping a SoundPlayer loop at the local level
Posted
by EvanRyan
on Stack Overflow
See other posts from Stack Overflow
or by EvanRyan
Published on 2010-05-26T20:24:49Z
Indexed on
2010/05/26
21:31 UTC
Read the original article
Hit count: 181
c#
|soundplayer
I'm working on setting up an alarm that pops up as a dialog with multiple sound file options the user can choose from. The problem I'm having is creating a sound player at the local level that I can close with a button. The problem I'm having is that the sound keeps looping when I close the form because the SoundPlayer doesn't exist within the button click event.
here's what I have:
void callsound()
{
if (SoundToggle == 0) // if sound enabled
{
if ((SoundFile == 0) && (File.Exists(@"attention.wav")))
{
System.Media.SoundPlayer alarm = new System.Media.SoundPlayer(@"attention.wav");
alarm.PlayLooping();
}
if ((SoundFile == 1) && (File.Exists(@"aahh.wav")))
{
System.Media.SoundPlayer alarm = new System.Media.SoundPlayer(@"aahh.wav");
alarm.PlayLooping();
}
}
private void button1_Click(object sender, EventArgs e)
{
//alarm.Stop(); Only works if SoundPlayer declared at class level
this.Close();
}
Is there a way I can do what I want to do by declaring the SoundPlayer instances where I am? Or is there a way to declare it at the class level, and still be able to change the sound file based on user settings?
© Stack Overflow or respective owner