c#播放海康mp4视频
时间: 2023-10-16 15:28:59 浏览: 175
你可以使用海康提供的 SDK (如 HCNetSDK 或者 EZ_SDK) 来播放海康的 MP4 视频。以下是一些示例代码,用于在 C# 中使用 HCNetSDK 播放海康 MP4 视频:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace HCNetSDKTest
{
public partial class Form1 : Form
{
// HCNetSDK.dll 的路径
const string HCNetSDKDllPath = "HCNetSDK.dll";
// 登录句柄
private int iLoginID = -1;
// 播放句柄
private int iPlayID = -1;
// 播放窗口
private IntPtr hWnd;
// SDK 初始化
[DllImport(HCNetSDKDllPath)]
private static extern bool NET_DVR_Init();
// SDK 释放
[DllImport(HCNetSDKDllPath)]
private static extern bool NET_DVR_Cleanup();
// 用户登录
[DllImport(HCNetSDKDllPath)]
private static extern int NET_DVR_Login_V30(string sDVRIP, ushort wDVRPort, string sUserName, string sPassword, ref NET_DVR_DEVICEINFO_V30 lpDeviceInfo);
// 用户注销
[DllImport(HCNetSDKDllPath)]
private static extern bool NET_DVR_Logout(int iLoginID);
// 打开实时预览
[DllImport(HCNetSDKDllPath)]
private static extern int NET_DVR_RealPlay_V40(int iUserID, ref NET_DVR_PREVIEWINFO lpPreviewInfo, REALDATACALLBACK fRealDataCallback_V30, IntPtr pUser);
// 关闭预览
[DllImport(HCNetSDKDllPath)]
private static extern bool NET_DVR_StopRealPlay(int iPlayHandle);
// 视频数据回调函数
public delegate void REALDATACALLBACK(int lRealHandle, uint dwDataType, IntPtr pBuffer, uint dwBufSize, IntPtr pUser);
// 预览参数结构体
public struct NET_DVR_PREVIEWINFO
{
public int lChannel;
public uint dwStreamType;
public uint dwLinkMode;
public IntPtr hPlayWnd;
public uint bBlocked;
public uint bPassbackRecord;
public uint byPreviewMode;
public uint byStreamID;
public byte[] byReserve;
}
// 设备信息结构体
[StructLayout(LayoutKind.Sequential)]
public struct NET_DVR_DEVICEINFO_V30
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 48)]
public string sSerialNumber;
public byte byAlarmInPortNum;
public byte byAlarmOutPortNum;
public byte byDiskNum;
public byte byDVRType;
public byte byChanNum;
public byte byStartChan;
public byte byAudioChanNum;
public byte byIPChanNum;
public byte byZeroChanNum;
public byte byMainProto;
public byte bySubProto;
public byte bySupport;
public byte bySupport1;
public byte byRes1;
public short wDevType;
public byte byRes2;
public byte bySupport2;
public ushort wFirmwareVersion;
public ushort wNetProtoVersion;
public ushort wUserProtoVersion;
public ushort wSignalVersion;
public ushort wIrdaVersion;
public byte byRes3;
public byte byHardwareVersion;
public byte bySoftwareVersion;
public byte byMiddleWareVersion;
public byte byBuildDate;
public byte byModifyDate;
public byte byRes4;
public byte byDevicePerformanceLevel;
public byte byReserve2;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// SDK 初始化
NET_DVR_Init();
// 获取播放窗口句柄
hWnd = pictureBox1.Handle;
// 登录
NET_DVR_DEVICEINFO_V30 devInfo = new NET_DVR_DEVICEINFO_V30();
iLoginID = NET_DVR_Login_V30("192.168.1.64", 8000, "admin", "12345", ref devInfo);
// 设置预览参数
NET_DVR_PREVIEWINFO previewInfo = new NET_DVR_PREVIEWINFO();
previewInfo.lChannel = 1; // 通道号
previewInfo.dwStreamType = 0; // 码流类型:0-主码流,1-子码流,2-码流3,3-码流4,以此类推
previewInfo.dwLinkMode = 0; // 连接模式:0-TCP,1-UDP,2-多播,3-RTP,4-RTP/RTSP,5-RSTP/HTTP
previewInfo.hPlayWnd = hWnd; // 播放窗口句柄
previewInfo.bBlocked = 1; // 阻塞取流:1-是,0-否
previewInfo.byPreviewMode = 0; // 预览模式:0-正常预览,1-延迟预览
previewInfo.byPassbackRecord = 0; // 是否启用录像回传:0-不启用,1-启用
previewInfo.byStreamID = 1; // 码流ID,取值范围为从0开始的32个整数
previewInfo.byReserve = new byte[216]; // 保留字段
// 打开实时预览
iPlayID = NET_DVR_RealPlay_V40(iLoginID, ref previewInfo, null, IntPtr.Zero);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// 关闭预览
NET_DVR_StopRealPlay(iPlayID);
// 注销
NET_DVR_Logout(iLoginID);
// SDK 释放
NET_DVR_Cleanup();
}
}
}
```
请注意,上述示例代码仅提供了基本的播放功能,并没有进行错误处理和异常处理。在实际应用中,您需要根据具体情况进行完善。
阅读全文