C# AxWindowsMediaPlayer loop
Asked Answered
W

2

7

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?

Wattle answered 16/11, 2013 at 19:36 Comment(1)
What is kind? and where is the try catch placed?Hesler
H
6

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();
    }
Hesler answered 16/11, 2013 at 21:28 Comment(4)
Thank you for your response, however it did not solve the problem. It did however give me a chance for better debugging. I debugged the playstates, but the playstate doesn't seem to be the problem. Somehow it exits the thread? This is my output: 3 8 9 3 The thread 0x1290 has exited with code 259 (0x103).Wattle
Okay, I found something weird. When I double click the video starts playing again...Wattle
Yeah your code is identical to mine now. In my code the 'kind' variable can be ignored. The video gets called only once, I debugged that so it's not a problem with double calling or something. I call this function from another program. This videoplayer is actually inside a .dll So the problem is still there, the video repeats once but pauses or stops on the last second...Wattle
All those hours.. djees. It was the format indeed, but why did it play twice and then stopped... So weird. Only 'bad' thing now is that it has a little bit of lag before starting the new loop. So it flickers a bit at times. But thanks for your help! it works now. If you know something that can help stop the flickering, that would be great.Wattle
J
12

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);
    }
Jeuz answered 17/6, 2017 at 5:44 Comment(1)
Hi, welcome to StackOverflow. Although the code you've pasted may be correct, you should include some explanation to make it even more useful. Can you do it?Shelashelagh
H
6

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();
    }
Hesler answered 16/11, 2013 at 21:28 Comment(4)
Thank you for your response, however it did not solve the problem. It did however give me a chance for better debugging. I debugged the playstates, but the playstate doesn't seem to be the problem. Somehow it exits the thread? This is my output: 3 8 9 3 The thread 0x1290 has exited with code 259 (0x103).Wattle
Okay, I found something weird. When I double click the video starts playing again...Wattle
Yeah your code is identical to mine now. In my code the 'kind' variable can be ignored. The video gets called only once, I debugged that so it's not a problem with double calling or something. I call this function from another program. This videoplayer is actually inside a .dll So the problem is still there, the video repeats once but pauses or stops on the last second...Wattle
All those hours.. djees. It was the format indeed, but why did it play twice and then stopped... So weird. Only 'bad' thing now is that it has a little bit of lag before starting the new loop. So it flickers a bit at times. But thanks for your help! it works now. If you know something that can help stop the flickering, that would be great.Wattle

© 2022 - 2024 — McMap. All rights reserved.