c# 海康威视 Winform播放mp4视频
最近有个视频播放系统,需要对海康的mp4格式视频进行播放,由于普通播放器无法对该视频进行播放原因是海康对视频进行了自己的编码,需要相应的解码才可以对视频进行播放。
下面是对海康威视视频播放的c#代码(需要从海康官网下载播放库)。
一,这个是核心库通过c#对海康提供的动态链接库dll方法的封装。
1 public class PlayCtrlSDK 2 { 3 #region 解码库 4 [SecurityCritical] 5 [DllImport("PlayCtrl.dll")] 6 public static extern uint PlayM4_GetLastError(int nPort); 7 /// <summary> 8 /// 获取文件当前播放位置(百分比)。 9 /// </summary> 10 /// <param name="nPort"></param> 11 /// <returns></returns> 12 [SecurityCritical] 13 [DllImport("PlayCtrl.dll")] 14 public static extern float PlayM4_GetPlayPos(int nPort); 15 /// <summary> 16 ///设置文件当前播放位置(百分比)。 17 /// </summary> 18 /// <param name="nPort"></param> 19 /// <returns></returns> 20 [SecurityCritical] 21 [DllImport("PlayCtrl.dll")] 22 public static extern float PlayM4_SetPlayPos(int nPort, float fRelativePos); 23 /// <summary> 24 /// 获取文件大小 25 /// </summary> 26 /// <returns></returns> 27 [SecurityCritical] 28 [DllImport("PlayCtrl.dll")] 29 public static extern uint PlayM4_GetFileHeadLength(); 30 /// <summary> 31 /// 获取文件总时间 32 /// </summary> 33 /// <param name="nPort"></param>PlayM4_GetPlayedTime 34 /// <returns></returns> 35 [SecurityCritical] 36 [DllImport("PlayCtrl.dll")] 37 public static extern uint PlayM4_GetFileTime(int nPort); 38 /// <summary> 39 /// 获取当前播放时间 40 /// </summary> 41 /// <param name="nPort"></param> 42 /// <returns></returns> 43 [SecurityCritical] 44 [DllImport("PlayCtrl.dll")] 45 public static extern uint PlayM4_GetPlayedTime(int nPort); 46 [SecurityCritical] 47 [DllImport("PlayCtrl.dll")] 48 public static extern int PlayM4_GetCaps(); 49 /// <summary> 50 /// 关闭声音 51 /// </summary> 52 /// <returns></returns> 53 [SecurityCritical] 54 [DllImport("PlayCtrl.dll")] 55 public static extern bool PlayM4_StopSound(); 56 /// <summary> 57 /// 获取未使用的通道号 58 /// </summary> 59 /// <param name="nPort"></param> 60 /// <returns></returns> 61 [SecurityCritical] 62 [DllImport("PlayCtrl.dll")] 63 public static extern bool PlayM4_GetPort(ref int nPort); 64 /// <summary> 65 ///播放声音 66 /// </summary> 67 /// <param name="nPort"></param> 68 [SecurityCritical] 69 [DllImport("PlayCtrl.dll")] 70 public static extern bool PlayM4_PlaySound(int nPort); 71 /// <summary> 72 /// 设置流播放模式 73 /// </summary> 74 /// <param name="port"></param> 75 /// <param name="mode"></param> 76 /// <returns></returns> 77 [SecurityCritical] 78 [DllImport("PlayCtrl.dll")] 79 public static extern bool PlayM4_SetStreamOpenMode(int nPort, int mode); 80 /// <summary> 81 /// 打开流 82 /// </summary> 83 /// <param name="nPort"></param> 84 /// <param name="pFileHeadBuf"></param> 85 /// <param name="nSize"></param> 86 /// <param name="nBufPoolSize"></param> 87 /// <returns></returns> 88 [SecurityCritical] 89 [DllImport("PlayCtrl.dll")] 90 public static extern bool PlayM4_OpenStream(int nPort, byte[] pFileHeadBuf, UInt32 nSize, uint nBufPoolSize); 91 /// <summary> 92 /// 设置播放缓冲区最大缓冲帧数 93 /// </summary> 94 /// <param name="nPort"></param> 95 /// <param name="nNum"></param> 96 /// <returns></returns> 97 [SecurityCritical] 98 [DllImport("PlayCtrl.dll")] 99 public static extern bool PlayM4_SetDisplayBuf(int nPort, uint nBufPoolSize); 100 /// <summary> 101 /// 打开文件 102 /// </summary> 103 /// <param name="nPort"></param> 104 /// <param name="hWnd"></param> 105 /// <returns></returns> 106 [SecurityCritical] 107 [DllImport("PlayCtrl.dll")] 108 public static extern bool PlayM4_OpenFile(int nPort, string fileName); 109 /// <summary> 110 /// 关闭文件 111 /// </summary> 112 /// <param name="nPort"></param> 113 /// <param name="hWnd"></param> 114 /// <returns></returns> 115 [SecurityCritical] 116 [DllImport("PlayCtrl.dll")] 117 public static extern bool PlayM4_CloseFile(int nPort); 118 /// <summary> 119 /// 开启播放 120 /// </summary> 121 /// <param name="nPort"></param> 122 /// <param name="hWnd"></param> 123 /// <returns></returns> 124 [SecurityCritical] 125 [DllImport("PlayCtrl.dll")] 126 public static extern bool PlayM4_Play(int nPort, IntPtr hWnd); 127 /// <summary> 128 /// 开始倒放 129 /// </summary> 130 /// <param name="nPort"></param> 131 /// <returns></returns> 132 [SecurityCritical] 133 [DllImport("PlayCtrl.dll")] 134 public static extern bool PlayM4_ReversePlay(int nPort); 135 /// <summary> 136 /// 输入流数据 137 /// </summary> 138 /// <param name="nPort"></param> 139 /// <param name="pBuf"></param> 140 /// <param name="nSize"></param> 141 /// <returns></returns> 142 [SecurityCritical] 143 [DllImport("PlayCtrl.dll")] 144 public static extern bool PlayM4_InputData(int nPort, byte[] pBuf, uint nSize); 145 /// <summary> 146 /// 关闭播放 147 /// </summary> 148 /// <param name="nPort"></param> 149 /// <returns></returns>PlayM4_Pause 150 [SecurityCritical] 151 [DllImport("PlayCtrl.dll")] 152 public static extern bool PlayM4_Stop(int nPort); 153 /// <summary> 154 ///暂停/播放1:暂停,0:恢复 155 /// </summary> 156 /// <param name="nPort"></param> 157 /// <returns></returns> 158 [SecurityCritical] 159 [DllImport("PlayCtrl.dll")] 160 public static extern bool PlayM4_Pause(int nPort, uint nPause); 161 /// <summary> 162 /// 关闭流 163 /// </summary> 164 /// <param name="nPort"></param> 165 /// <returns></returns> 166 [SecurityCritical] 167 [DllImport("PlayCtrl.dll")] 168 public static extern bool PlayM4_CloseStream(int nPort); 169 /// <summary> 170 /// 释放已使用的通道号 171 /// </summary> 172 /// <param name="nPort"></param> 173 /// <returns></returns> 174 [SecurityCritical] 175 [DllImport("PlayCtrl.dll")] 176 public static extern bool PlayM4_FreePort(int nPort); 177 178 #endregion 179 }
二,自定义控件:如下图
三。引用控件实现效果如下:
实现功能很简单,主要是对海康播放库的调用。自己做的这个demo只用到了几个播放,暂停,快进,后退,播放时间,当前时间几个功能。