Problems with music in Windows Forms in C#

Asked

Viewed 86 times

1

I am doing a job for college with Object Orientation and Windows Forms, but I have the following problem:

The program consists of several Forms, in the first I made the code to play music as shown in the code below

 private void Form1_Load(object sender, EventArgs e)
    {
        som = Path.Combine(CaminhoMusica, "MenuSom.mp3");
        WMPLib.WindowsMediaPlayer musica = new WMPLib.WindowsMediaPlayer();
        musica.URL = som;
        musica.controls.play();
        musica.settings.setMode("loop", true);
        guardar.Tocandomusica = true;
    }

however, when I open any other filters that have the Load event, such as this

 private void Habitos_Load(object sender, EventArgs e)
    {

        if (animal is Cachorro)
        {
            lbAnimal.Text = "Cachorro";
            lbClassificacao.Text = "Mamifero - Predador";
            pnMamiferos.Enabled = true;
            pnPredadores.Enabled = true;
        }
    }

at the end of the load event, the music stops playing in the entire program, and this is something that happens only in the Forms that have a load event, in the others the music continues playing normally, what I can do to solve this?

EDIT:Debugging the code, I realized that the problem is not in the load event but in

            Habitos habitos = new Habitos(x.Valor);
            this.Hide();
            habitos.ShowDialog();
            this.Show();

on the habitos line.Showdialog();,but I still don’t know what is causing this, because it is only in some specific Forms.

EDIT2: I solved the problem, I had to instantiate Wmplib.Windowsmediaplayer musica = new Wmplib.Windowsmediaplayer(); outside the Load event of the first form, because it seems to me that after opening a certain number of Forms, the load of the first one was finished and so the music object was deleted together

  • Hello @Renato. Edit your question and put the code in formatted text instead of image.

  • I edited the question

  • What is Wmplib?

  • I don’t know how to explain technically what it is, but this Wmplib.Windowsmediaplayer serves to play sounds in windows media player via code

  • Anyway, I’ve solved the problem, I’ll edit the question with the answer in case someone has the same problem

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.