本文提供 HikPlayM4.dll 核心播放功能的 C# P/Invoke 封裝類別,涵蓋檔案開啟、播放控制、音訊、畫質、截圖、回調註冊等常用 API,供系列後續文章直接引用。
using System;
using System.Runtime.InteropServices;
namespace HikPlayer
{
public static class HikPlayM4
{
// 播放通道常數
public const int MAX_PORT = 16;
// 視訊格式枚舉
public enum VideoFormat
{
vdfYUV422Planar = 0,
vdfRGB16 = 1,
vdfRGB24 = 2,
vdfRGB32 = 3,
vdfYUV420Planar = 4
}
// 核心 API 宣告
[DllImport("HikPlayM4.dll")]
public static extern bool Hik_PlayM4_OpenFile(int nPort, string sFileName);
[DllImport("HikPlayM4.dll")]
public static extern bool Hik_PlayM4_Play(int nPort, IntPtr hWnd);
[DllImport("HikPlayM4.dll")]
public static extern bool Hik_PlayM4_Stop(int nPort);
[DllImport("HikPlayM4.dll")]
public static extern bool Hik_PlayM4_Pause(int nPort, bool bPause);
[DllImport("HikPlayM4.dll")]
public static extern bool Hik_PlayM4_Fast(int nPort);
[DllImport("HikPlayM4.dll")]
public static extern bool Hik_PlayM4_Slow(int nPort);
[DllImport("HikPlayM4.dll")]
public static extern bool Hik_PlayM4_SetPlayPos(int nPort, double dPos); // 0.0~1.0
[DllImport("HikPlayM4.dll")]
public static extern bool Hik_PlayM4_PlaySound(int nPort);
[DllImport("HikPlayM4.dll")]
public static extern bool Hik_PlayM4_StopSound();
[DllImport("HikPlayM4.dll")]
public static extern bool Hik_PlayM4_SetVolume(int nPort, int nVolume); // 0~0xFFFF
[DllImport("HikPlayM4.dll")]
public static extern bool Hik_PlayM4_GetPictureSize(int nPort, out int nWidth, out int nHeight);
[DllImport("HikPlayM4.dll")]
public static extern bool Hik_PlayM4_SetOverlayMode(int nPort, bool bEnable, int nColorKey);
[DllImport("HikPlayM4.dll")]
public static extern bool Hik_PlayM4_SetDisplayCallBack(int nPort, DisplayCBFun fDisplayCBFun);
[DllImport("HikPlayM4.dll")]
public static extern bool Hik_PLayM4_ConvertToBmpFile(IntPtr pBuf, int nSize, int nWidth, int nHeight, int nType, string sFileName);
[DllImport("HikPlayM4.dll")]
public static extern bool Hik_PlayM4_CloseFile(int nPort);
[DllImport("HikPlayM4.dll")]
public static extern bool Hik_PlayM4_RealeseDDraw();
[DllImport("HikPlayM4.dll")]
public static extern int Hik_PlayM4_GetLastError(int nPort);
[DllImport("HikPlayM4.dll")]
public static extern bool Hik_PlayM4_ThrowBFrameNum(int nPort, int nNum);
[DllImport("HikPlayM4.dll")]
public static extern bool Hik_PlayM4_SetPicQuality(int nPort, bool bHighQuality);
// 回調委派定義
public delegate void DisplayCBFun(int nPort, IntPtr pBuf, int nSize, int nWidth, int nHeight, int nStamp, int nType, int nReceaved);
// 視窗訊息常數
public const int WM_FILE_END = 0x0400 + 100;
public const int WM_ENC_CHANGE = 0x0400 + 101;
}
}
bool,false 表示失敗,可呼叫 GetLastError 取得錯誤碼。nPort 範圍 0~15,同一時間每個 Port 只能播放一個來源。SetDisplayCallBack 設為 null 可停止回調。CloseFile、Stop、RealeseDDraw 釋放資源。本封裝僅包含播放器核心功能,隨系列文章推進會持續補充(如解碼回調、音訊回調、流播放等)。建議專案中建立獨立 HikPlayer 專案引用,便於維護與單元測試。