Search code examples
c#winformsvisual-studio-2013playlistaxwindowsmediaplayer

Delay next item in the axWindowsMediaPlayer playlist until the current item has completed playing


I have used the following code to create and loop my videos in my playlist:

private void Form1_Load(object sender, EventArgs e)
{
   var pl = axWindowsMediaPlayer1.playlistCollection.newPlaylist("plList");
   pl.appendItem(axWindowsMediaPlayer1.newMedia(@"C:\ABC\abc.mp4"));
   pl.appendItem(axWindowsMediaPlayer1.newMedia(@"C:\XYZ\xyz.mp4"));

   axWindowsMediaPlayer1.currentPlaylist = pl;            
   //axWindowsMediaPlayer1.settings.setMode("loop", true);            
   axWindowsMediaPlayer1.Ctlcontrols.play();
}

However, I found out that the next item in the playlist plays for 2 seconds before the end of current item in the playlist. Then, the new item plays all over again (i.e. the first 2 seconds of the item plays twice, once before the end of current item and once at the beginning of the next item).

How do I ensure that the next item is loaded only after the current item is complete playing?

Requirement 2: How do I detect the end of Playlist (i.e. After playing of all videos)

I tried to capture it in PlayStateChange Event, but I am unable to capture it in either in Media Ended or Stopped or Last states


Solution

  • To perfectly loop your videos, use the following:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            [DllImport("user32.dll")]
            public static extern Boolean GetLastInputInfo(ref tagLASTINPUTINFO plii);
            public struct tagLASTINPUTINFO
            {
                public uint cbSize;
                public Int32 dwTime;
            }
    
            public Form1()
            {
                InitializeComponent();
                timer1.Start();
            }        
    
            private void Form1_Load(object sender, EventArgs e)
            {
                axWindowsMediaPlayer1.Ctlenabled = true;
                var pl = axWindowsMediaPlayer1.playlistCollection.newPlaylist("XYZ");
                p1.appendItem(axWindowsMediaPlayer1.newMedia(@"C:\Folder\file1.mp4"));
                pl.appendItem(axWindowsMediaPlayer1.newMedia(@"C:\Folder\file2.mp4"));
                axWindowsMediaPlayer1.currentPlaylist = pl;
                axWindowsMediaPlayer1.Ctlcontrols.play();
            }            
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                tagLASTINPUTINFO LastInput = new tagLASTINPUTINFO();
                Int32 IdleTime;
                LastInput.cbSize = (uint)Marshal.SizeOf(LastInput);
                LastInput.dwTime = 0;
    
                if (GetLastInputInfo(ref LastInput))
                {
                    IdleTime = System.Environment.TickCount - LastInput.dwTime;
                    if (IdleTime > 10000)
                    {
                        axWindowsMediaPlayer1.Ctlcontrols.pause();
                        timer1.Stop();
                        MessageBox.Show("Do you wish to continue?");
                    }
                    timer1.Start();
                    axWindowsMediaPlayer1.Ctlcontrols.play();
                }
            }
        }
    }