Home > database >  Making music stop after form is closed and play when a form is loaded
Making music stop after form is closed and play when a form is loaded

Time:02-22

For the application that i am creating i am trying to add in music which plays when form2 is opened. which seems to work just fine, the only issue i have is when i close the form it immediately starts playing the music again. Here is my code:

        private void Form2_Load(object sender, EventArgs e)
        {
            music(true);
        }
        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            music(false);
        }
        private void music(bool placeholder)
        {
            System.IO.Stream str = Properties.Resources.Music;
            System.Media.SoundPlayer snd = new System.Media.SoundPlayer(str);
            if (placeholder = true)
            {
                snd.Play();
            } else
            {
                snd.Stop();
            }
        }

if i start my application it doesnt play any music which it should since it opens on form1. When i open form2 it plays the music as it should but when i close it, it restarts the music and it just keeps playing.

CodePudding user response:

It was a simple error of judgment.

if (placeholder = true)
            {
                snd.Play();
            } else
            {
                snd.Stop();
            }

I suggest that you modify the above code to:

if (placeholder == true)
            {
                snd.Play();
            } else
            {
                snd.Stop();
            }

Please comment below if it doesn't work.

  • Related