Pregunta

I am once again a bit stuck in my practising. I want an MP3 file to play when i open my program - I can do that, i got music. I also want a checkbox which allows to pause the music - But either I'm very tired, or the thing won't work - Nothing happens when i check/uncheck it. I've done it like this:

public void PlayPause(int Status)
     {
        WMPLib.WindowsMediaPlayer wmp = new WMPLib.WindowsMediaPlayer();
        switch (Status)
        {
            case 0:
                wmp.URL = "Musik.mp3";
                break;
            case 1:
                wmp.controls.play();
                break;
            case 2:
                wmp.controls.pause();
                break;
        }
    }

Upon opening the program, the method is called with case 0. Music plays. All good. However this doesn't work, and i don't get why, as it is pretty simple code.

        public void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked == true)
        {
            PlayPause(2);
        }
        else if (checkBox1.Checked == false)
        {
            PlayPause(1);
        }
    }

Any idea as to why checking the checkbox doesn't pause/unpause the music?

¿Fue útil?

Solución

You're instantiating a completely new WindowsMediaPlayer object each time you call that PlayPause function.

Thus, when you call pause later on, you're pausing nothing.

You need to hold or pass a reference to that WMP object around, so that you're operating on the same one.

Otros consejos

Well it's because you are creating a new media player every time you call PlayPause. Create it in the constructor and it should be fine.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top