Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# AxWindowsMediaPlayer loop

Tags:

c#

loops

wmp

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?

like image 954
Serellyn Avatar asked Nov 16 '13 19:11

Serellyn


2 Answers

just use

    private void Form1_Load(object sender, EventArgs e)
    {
        // give the path of your video here
        axWindowsMediaPlayer1.URL = "Path of your video";
        // this line will automatically start your video
        axWindowsMediaPlayer1.settings.autoStart = true;
        //here the system will automatially create a thread and will keep on 
         running your video...
        axWindowsMediaPlayer1.settings.setMode("loop", true);
    }
like image 184
Farrukh Avatar answered Oct 10 '22 15:10

Farrukh


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();
    }
like image 24
Jerry Avatar answered Oct 10 '22 14:10

Jerry