Search code examples
c#formsbuttonsoundplayer

Pause/ Resume background music in c#


I have a background music and i want to be able to pause/unpause it or mute/unmute it with the press of one button.Anyone any suggestions? Thanks in advance. This is what I have until now, but i can't get it working;

edit: I used WMP because you can't pause in SoundPlayer; then made a bool and set it to false and then used this loop to get the button working

 if (paused == false)
        {
            MusicPlayer1.controls.pause();
            paused = true;
        }
        else
        {
            MusicPlayer1.controls.play();
            paused = false;
        }

/

    public Game()
    {
        GameHeigth = 45;   // some other stuff
        GameWidth = 45;
        matrix = new Cube[15, 15];
        XObjects = new List<VObject>();
        rnd = new Random();

        InitializeComponent();
        GenerateField();
        NeighbourBase();
        StartGame();
        Muziek();

    }


  private void Muziek()
    {
        System.Media.SoundPlayer player1 = new System.Media.SoundPlayer("liedje.wav");
        player1.PlayLooping();
    }

 private void PauseResumeButton_Click(object sender, EventArgs e)
    {
        Muziek.Player1.Pause(); //this doenst work

    }

Solution

  • I'm not sure if you are using a WPF program or not, but if you can try using a Media Element instead of the SoundPlayer you are using. The SoundPlayer is very limited and does not have very many features when compared to the Media Elements that C# also provides.