首页 > 代码库 > [Winform]Media Player播放控制面板控制,单击事件截获

[Winform]Media Player播放控制面板控制,单击事件截获

摘要

在项目中有这样的一个需求,需要在一台宣传机器上循环播放一段视频,并在体验的用户单击鼠标左键的时候推出全屏,可以让用户体验电脑的其它功能。

解决方案

考虑到都是windows系统的,所以采用了windows自带的播放器,Windows Media Player Com组件作为视频播放器。

如图

技术分享

但是,为了更好满足需求,下面的播放控制面板需要隐藏,该怎么做呢?最后找到解决方案,设置播放器属性

windowsMediaPlay.uiMode = "none";

循环播放的代码,这里为播放器添加播放列表,代码如下:

 string[] filePaths = Directory.GetFiles(_videoDirPath).Where(x => x.EndsWith(".mp4") || x.EndsWith(".wmv")).ToArray();                //添加循环播放列表                foreach (var item in filePaths)                {                    windowsMediaPlay.currentPlaylist.appendItem(windowsMediaPlay.newMedia(item));                }                //不再任务栏显示                this.ShowInTaskbar = false;                this.FormClosing += VideoFrm_FormClosing;                windowsMediaPlay.ClickEvent += windowsMediaPlay_ClickEvent;                windowsMediaPlay.KeyUpEvent += windowsMediaPlay_KeyUpEvent;                windowsMediaPlay.StatusChange += windowsMediaPlay_StatusChange;                windowsMediaPlay.ErrorEvent += windowsMediaPlay_ErrorEvent;                windowsMediaPlay.DoubleClickEvent += windowsMediaPlay_DoubleClickEvent;                windowsMediaPlay.Ctlcontrols.play();

那么,如何在用户单击的时候,让程序最小化?

注意

在uiMode = "none"的情况下,单击会触发视频暂停的事件,并不会退出。视频暂停,是不是视频状态变化了呢?所以在截获单击视频事件,我们完全可以在视频暂停的时候,对其进行截获。statechange事件对应的方法如下:

       void windowsMediaPlay_StatusChange(object sender, EventArgs e)        {            /*               * 0 Undefined Windows Media Player is in an undefined state.(未定义)                1 Stopped Playback of the current media item is stopped.(停止)                2 Paused Playback of the current media item is paused. When a media item is paused, resuming 
playback begins from the same location.(停留) 3 Playing The current media item is playing.(播放) 4 ScanForward The current media item is fast forwarding. 5 ScanReverse The current media item is fast rewinding. 6 Buffering The current media item is getting additional data from the server.(转换) 7 Waiting Connection is established, but the server is not sending data. Waiting for session to begin.(暂停) 8 MediaEnded Media item has completed playback. (播放结束) 9 Transitioning Preparing new media item. 10 Ready Ready to begin playing.(准备就绪) 11 Reconnecting Reconnecting to stream.(重新连接)
*/ try { switch (windowsMediaPlay.playState) { case WMPLib.WMPPlayState.wmppsBuffering: break; case WMPLib.WMPPlayState.wmppsLast: break; case WMPLib.WMPPlayState.wmppsMediaEnded: break; case WMPLib.WMPPlayState.wmppsPaused: //单击了视频则退出 HideVideo(); break; case WMPLib.WMPPlayState.wmppsPlaying: if (!windowsMediaPlay.fullScreen) { windowsMediaPlay.fullScreen = true; } break; case WMPLib.WMPPlayState.wmppsReady: break; case WMPLib.WMPPlayState.wmppsReconnecting: break; case WMPLib.WMPPlayState.wmppsScanForward: break; case WMPLib.WMPPlayState.wmppsScanReverse: break; case WMPLib.WMPPlayState.wmppsStopped: break; case WMPLib.WMPPlayState.wmppsTransitioning: break; case WMPLib.WMPPlayState.wmppsUndefined: break; case WMPLib.WMPPlayState.wmppsWaiting: break; default: break; } } catch (Exception ex) { LogInfoData.WriteLog(new LogInfo { Dt = DateTime.Now, IsSend = false, Message = ex.Message, Op = "media_state_change_err" }); } }

关于双击全屏截获的逻辑也可以在这里处理,双击之前肯定会有一次单击,所以在这里也能进行双击最大化的处理。 

[Winform]Media Player播放控制面板控制,单击事件截获