Search code examples
c#winformsvisual-studio-2013timeraxwindowsmediaplayer

timer1_Tick event in WinForms C# in presence of axWindowsMediaPlayer


Note: This application will be designed for a touch device (MS Surface Hub)

My Windows Form contains axWindowsMediaPlayer component. I have created a playlist and I am able to loop the media files in the playlist. However, I want my axWindowsMediaPlayer playlist to pause after 5 sec (time limit just for testing/debugging purpose) of inactivity (No input from the user to be more precise) and display a dialog-box asking me whether I wish to continue.

Following is my code to set a timer_Tick event:

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 TimerDemo
{
  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();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        axWindowsMediaPlayer1.Ctlenabled = true;
        var pl = axWindowsMediaPlayer1.playlistCollection.newPlaylist("MyPlaylist");
        pl.appendItem(axWindowsMediaPlayer1.newMedia(@"C:\ABC\abc1.mp4"));
        pl.appendItem(axWindowsMediaPlayer1.newMedia(@"C:\ABC\abc2.mp4"));
        axWindowsMediaPlayer1.currentPlaylist = pl;
        axWindowsMediaPlayer1.Ctlcontrols.play();
    }

    private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
    {
        if (e.newState == 8)   //Media Ended
        {                
        }
    }

    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 > 5000)
            {
                axWindowsMediaPlayer1.Ctlcontrols.pause();
                timer1.Stop();
                MessageBox.Show("Do you wish to continue?");
            }
            else
            {
            }
            timer1.Start();
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }
    }
  }
}

With this code, the application is not entering the timer1_Tick Event.

Queries:

  1. Does e.newState == 3 (Playing State) in axWindowsMediaPlayer considered as input?
  2. How do I ensure that the application enters into timer1_Tick event?

If I remove the axWindowsMediaPlayer part of the code then the timer1_Tick event is responding.


Solution

  • For your application to get into the timer_Tick event, you first need to start the timer.

    Replace the following code:

    public Form1()
    {
        InitializeComponent();
    }
    

    With the Following:

    public Form1()
    {
        InitializeComponent();
        timer1.Start();
    }
    

    This should work fine for you.