سؤال

        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