Reputation: 545
I am trying to create my own little Soundboard for playing WAV files. Now the playback isn't a problem, but I want a way to stop playback if required. I thought of a stop button, but did not know how to make that work. So I thought of using a loop but it doesn't seem to work. Just wondering if anyone can help me where I am going wrong or if this is just a fail way to do it, then if you can show me a better way that would be great. :)
private void button1Sound(object sender, MouseEventArgs e)
{
int i = 0;
SoundPlayer simpleSound = new SoundPlayer(@"file.wav");
while (i == 0)
{
simpleSound.Play();
i++;
}
if (i >= 1)
{
simpleSound.Stop();
}
}
I also thought of just doing an ELSE IF but that also didn't work. I tried:
private void button1Sound(object sender, MouseEventArgs e)
{
int i = 0;
SoundPlayer simpleSound = new SoundPlayer(@"file.wav");
if (i == 0)
{
simpleSound.Play();
i++;
}
else if (i > 1)
{
simpleSound.Stop();
}
}
Upvotes: 4
Views: 5926
Reputation: 697
EDIT - editing code fragment to show use of Asynchronous Load
Try this code:
private System.Media.SoundPlayer sound;
private bool isPlaying = false;
private System.Drawing.Image playimage = System.Drawing.Image.FromFile(@"playimage");
private System.Drawing.Image stopimage = System.Drawing.Image.FromFile(@"stopimage");
private void playstopbutton_Click(object sender, EventArgs e)
{
Button btn = (sender as Button);
if (isPlaying)
{
sound.Stop();
isPlaying = false;
btn.Text = "Play";
btn.Image = playimage;
}
else if (sound.IsLoadCompleted)
{
sound.Play();
isPlaying = true;
btn.Text = "Stop";
btn.Image = stopimage;
}
else
{
btn.Enabled = false;
if (sound == null) sound = new System.Media.SoundPlayer(@"wavefile");
sound.LoadCompleted += new AsyncCompletedEventHandler(sound_LoadCompleted);
sound.LoadAsync();
}
}
private void sound_LoadCompleted(object sender, AsyncCompletedEventArgs e)
{
this.playstopbutton.Text = "Stop";
this.playstopbutton.Image = playimage;
this.playstopbutton.Enabled = true;
isPlaying = true;
sound.Play();
}
The problem you might be running into is your scope for the sound is only inside the method, it needs to be class scope, so that the same instance sound is dealt with each time. Technically the int method you used shuold work if you declare the SoundPlayer outside the click handler. Also, whatever variable you use to track the play state needs to exist outside the handler method so that the value persists on each click action.
Upvotes: 2
Reputation: 5571
Because you've added int i = 0
inside the void, every time the event is called, i
will always be 0. So it's better to include i
outside the void.
int i = 0;
SoundPlayer simpleSound = new SoundPlayer(@"file.wav"); //Thanks to @Wanabrutbeer
private void button1Sound(object sender, MouseEventArgs e)
{
if (i == 0)
{
simpleSound.Play();
i++;
}
else if (i >= 1)
{
simpleSound.Stop();
i--;
}
}
NOTICE: The class SoundPlayer
only plays .wav files. If you would like to play other media files such as .mp3, .mpeg, etc... please use MediaPlayer
instead.
Thanks,
I hope this helps :)
Upvotes: 3