I have seen a few posts about multi-function buttons here and elsewhere, but I am having trouble implementing their suggestions.
I am coding a synthesizer that generates and saves a .wav file. I have a button that plays the sound with SoundPlayer 'without saving it' by saving it to 'bin', and I delete it later. I realise this isn't an ideal way of playing sounds!
I've got the button label changing from "Play" to "Stop" when it is clicked. Unfortunately, I can't get the button to function as 'stop'.
I have tried creating a 'counter',
public static int NumOfClicks = 0;
and doing NumOfClicks++ each time the button is clicked, then if(IsEven(NumOfClicks)), but it gives the error 'IsEven does not exist in this context'.
private void btnPlay_Click_1(object sender, EventArgs e)
{
NumOfClicks++;
btnPlay.Text = "Stop";
filePath = @"TestTone.wav";
WaveGenerator wave = new WaveGenerator();
wave.Save(filePath);
SoundPlayer player = new SoundPlayer(filePath);
player.Play();
if (IsEven(NumOfClicks))
{
btnPlay.Text = "Play";
player.Stop();
filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
File.Delete(@"TestTone.wav");
}
}
I tried creating a separate 'stop' button, but then the player.Stop() command doesn't work (player 'does not exist in this context').
I tried using a switch with NumOfClicks but got the same 'player does not exist in this context' error.
If anyone has a fix or a better way of doing this I would greatly appreciate it!