1

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

    }
henk_bae
  • 25
  • 7
  • Maybe see [this question](http://stackoverflow.com/questions/19762734/c-sharp-using-soundplayer-to-pause-and-resume) – John Wu Mar 21 '17 at 23:37

2 Answers2

1

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.

Pacific Bird
  • 286
  • 1
  • 3
  • 13
0

Your issue is probably to do with the scope of the player1 object here. player1 is not available outside your Muziek method, try declaring it as a public field and initialize it within the Muziek method. but not enough information in your question to help further.

Ahsan
  • 2,488
  • 2
  • 22
  • 44
  • I declared it as public but it still doesn't work, I get 'The name 'player1' does not exist in the current context'. Could you give me an concrete example on how i could do what you suggest please? – henk_bae Mar 21 '17 at 23:37
  • Now that I have looked at the 'System.Media.SoundPlayer' it does not have a pause method. That's a limitation. You will need to use a different framework. are you using WPF? – Ahsan Mar 21 '17 at 23:49
  • I indeed used Windows Media Player and got the first part working, now i got to figure out how to program the button to resume/pause . – henk_bae Mar 22 '17 at 00:15