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

    }
4

2 回答 2

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.

于 2017-03-21T23:26:47.200 回答
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.

于 2017-03-21T23:29:15.763 回答