timer1_Tick WinForms C# 中存在 axWindowsMediaPlayer 的事件

timer1_Tick event in WinForms C# in presence of axWindowsMediaPlayer

注意:此应用程序将专为触摸设备 (MS Surface Hub) 而设计

我的 Windows 表单包含 axWindowsMediaPlayer 组件。我已经创建了一个播放列表,并且可以循环播放列表中的媒体文件。但是,我希望我的 axWindowsMediaPlayer 播放列表在 5 秒(仅用于 testing/debugging 目的的时间限制)不活动(没有来自用户的输入是更精确)并显示一个对话框询问我是否要继续。

以下是我设置 timer_Tick 事件的代码:

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

使用此代码,应用程序不会进入 timer1_Tick 事件。

查询:

  1. axWindowsMediaPlayer中的e.newState == 3(播放状态)是否被视为输入?
  2. 如何确保应用程序进入timer1_Tick事件?

如果我删除代码的 axWindowsMediaPlayer 部分,那么 timer1_Tick 事件就会响应。

为了让您的应用程序进入 timer_Tick 事件,您首先需要启动 计时器

替换以下代码:

public Form1()
{
    InitializeComponent();
}

与以下内容:

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

这应该适合你。