SoundPlayer StartUpMusic = new SoundPlayer(Resources.Guiles_Theme);       
        private void MuteButton_Click(object sender, EventArgs e)
        {
            if (StartUpMusic.IsLoadCompleted == true)
            {
                StartUpMusic.Stop();
                StartUpMusic.Dispose();
            }
            else
            {
                StartUpMusic.Load();
                StartUpMusic.Play();
            }
        }

This is an event triggered when the user clicks the play button. I think my condition within the if statement is not good. I basically want the sound to be muted when the button is pressed. Then I want the sound to continue when the button is pressed and the sound is already muted. What is wrong here? You time and effort are greatly appreciated. Thank you!

有帮助吗?

解决方案

Just use a boolean flag to indicate if the sound is playing or not. So something like this might work:

private boolean isPlaying = true;
private void MuteButton_Click(object sender, EventArgs e)
{
    if (StartUpMusic.IsLoadCompleted)
    {
        if (isPlaying)
            StartUpMusic.Stop();
        else
            StartUpMusic.Play();
        isPlaying = !isPlaying;
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top