Search code examples
c#audiomute

Any way to mute the sound yet keep it running in the background instead of stopping it completely?


What the title says really, Right now I only know how to control the sound by using .Play and .Stop, however is there a way to mute the sound but still have it playing in the background?

Here is a snippet of the code:

    private void muteButton_Click(object sender, EventArgs e)
    {
        if (muteButton.Text == "Mute")
        {

            muteButton.Text = "Unmute";
            _soundPlayer.Stop();
        }

        else
        {
            muteButton.Text = "Mute";
            _soundPlayer.PlayLooping();
        }
        }

Solution

  • You should try this

    private boolean playing = true;
    private void muteButton_Click(object sender, EventArgs e)
    {
        if (_soundPlayer.IsLoadCompleted)
        {
            if (playing)
               {
                _soundPlayer.Stop();
                playing = false;
                }
            else
                {
                _soundPlayer.Play();
                 playing = true;
                }
        }
    }