Search code examples
c#loopswmp

C# AxWindowsMediaPlayer loop


I've got this annoying problem which I can't track down where it goes wrong. I'm creating a Windows Media Player in code and I'm trying to loop a video... It loops, but only once...

So it plays the video, and once more. And then it just stop and shows the end of the video. So it seems as if it loops only once.

This is the code I have:

        try {
            wmPlayer = new AxWMPLib.AxWindowsMediaPlayer();

            wmPlayer.enableContextMenu = false;
            ((System.ComponentModel.ISupportInitialize)(wmPlayer)).BeginInit();
            wmPlayer.Name = "wmPlayer";
            wmPlayer.Enabled = true;
            wmPlayer.Dock = System.Windows.Forms.DockStyle.Fill;
            mainForm.Controls.Add(wmPlayer);
            ((System.ComponentModel.ISupportInitialize)(wmPlayer)).EndInit();
            wmPlayer.uiMode = "none";

            if(kind == "idle") {
                IdleVideo(name);
            }
        }
        catch { }
    }

    private static void IdleVideo(string name) {
        System.Diagnostics.Debug.WriteLine("Video called once");
        wmPlayer.URL = @"C:\ProjectSilver\assets\RadarDetectie\idle\" + name + "_idlescreen_movie.ogv";
        Debug.WriteLine(wmPlayer.URL);
        wmPlayer.settings.setMode("loop", true);

        wmPlayer.Ctlcontrols.play();
    }

So I hope you guys can help, why doesn't it keep playing?


Solution

  • Add an event handler for the PlayStateChange event:

    wmPlayer.PlayStateChange += wmPlayer_PlayStateChange;
    

    Then in the event handler check if e.newState==8 which means media ended:

    AxWMPLib.AxWindowsMediaPlayer wmPlayer;
    private void player_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
    {
       if(e.newState==8) // MediaEnded
            // call function to play the video again     
    }
    

    For play states, check this: http://msdn.microsoft.com/en-us/library/windows/desktop/dd562460%28v=vs.85%29.aspx

    Edit: I don't know what you do with kind, or where the first part of your code is defined, but this worked for me:

    AxWMPLib.AxWindowsMediaPlayer wmPlayer;
    
    private void button2_Click(object sender, EventArgs e)
        {
            wmPlayer = new AxWMPLib.AxWindowsMediaPlayer();
            wmPlayer.CreateControl();
            wmPlayer.enableContextMenu = false;
            ((System.ComponentModel.ISupportInitialize)(wmPlayer)).BeginInit();
            wmPlayer.Name = "wmPlayer";
            wmPlayer.Enabled = true;
            wmPlayer.Dock = System.Windows.Forms.DockStyle.Fill;
            this.Controls.Add(wmPlayer);
            ((System.ComponentModel.ISupportInitialize)(wmPlayer)).EndInit();
            wmPlayer.uiMode = "none";
            wmPlayer.URL = @"C:\...";
            wmPlayer.settings.setMode("loop", true);
    
            wmPlayer.Ctlcontrols.play();
        }