📄 unitplayback.~pas
字号:
unit UnitPlayBack;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, Grids, StdCtrls, ComCtrls,DateUtils;
type
TfrmPlayBack = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
VideoPanel: TPanel;
StringGrid1: TStringGrid;
ComboBox1: TComboBox;
Label1: TLabel;
Label2: TLabel;
ComboBox2: TComboBox;
DateTimePicker1: TDateTimePicker;
DateTimePicker2: TDateTimePicker;
DateTimePicker3: TDateTimePicker;
DateTimePicker4: TDateTimePicker;
Label3: TLabel;
Label4: TLabel;
cmdSearch: TButton;
Button2: TButton;
cmdPlay: TButton;
cmdStop: TButton;
txtFilePath: TEdit;
cmdDownFile: TButton;
ProgressBar1: TProgressBar;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure cmdSearchClick(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure cmdPlayClick(Sender: TObject);
procedure cmdStopClick(Sender: TObject);
procedure cmdDownFileClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmPlayBack: TfrmPlayBack;
m_lFileHandle:LongInt;
m_hFileThread:THandle;
m_PlayIndex:LongInt;
m_bStartDownload:Boolean;
implementation
uses HikVisionSDK, Unit1;
{$R *.dfm}
procedure GetFileThread(p:Pointer);stdcall;
var
FileInfo:NET_DVR_FIND_DATA;
pFileInfo:LPNET_DVR_FIND_DATA;
lRet :LongInt;
tempstring:AnsiString;
begin
pFileInfo :=@FileInfo;
while True do
begin
ZeroMemory(@FileInfo,SizeOf(NET_DVR_FIND_DATA));
lRet := NET_DVR_FindNextFile(m_lFileHandle, pFileInfo);
if lRet= NET_DVR_FILE_SUCCESS then
begin
//
frmPlayBack.StringGrid1.Cells[1,frmPlayBack.StringGrid1.RowCount-1]:=FileInfo.sFileName; //文件名
frmPlayBack.StringGrid1.Cells[2,frmPlayBack.StringGrid1.RowCount-1]:=IntToStr(FileInfo.dwFileSize); //大小
tempstring:=Format('%d-%d-%d %d:%d:%d',[FileInfo.struStartTime.dwYear, FileInfo.struStartTime.dwMonth,FileInfo.struStartTime.dwDay,FileInfo.struStartTime.dwHour,FileInfo.struStartTime.dwMinute,FileInfo.struStartTime.dwSecond]);
frmPlayBack.StringGrid1.Cells[3,frmPlayBack.StringGrid1.RowCount-1]:=tempstring; //开始时间
tempstring:=Format('%d-%d-%d %d:%d:%d',[FileInfo.struStopTime.dwYear, FileInfo.struStopTime.dwMonth,FileInfo.struStopTime.dwDay,FileInfo.struStopTime.dwHour,FileInfo.struStopTime.dwMinute,FileInfo.struStopTime.dwSecond]);
frmPlayBack.StringGrid1.Cells[4,frmPlayBack.StringGrid1.RowCount-1]:=tempstring; //结束时间
frmPlayBack.StringGrid1.RowCount:=frmPlayBack.StringGrid1.RowCount+1;
end
else if lRet = NET_DVR_ISFINDING then
begin
Sleep(10);
continue;
end
else if (lRet = NET_DVR_NOMOREFILE) or (lRet = NET_DVR_FILE_NOFIND) then
begin
if frmPlayBack.StringGrid1.RowCount>2 then
frmPlayBack.StringGrid1.RowCount:=frmPlayBack.StringGrid1.RowCount-1;
MessageBox(0,'获取文件列表结束,文件已经全部列出!','完成',MB_OK+64);
frmPlayBack.cmdSearch.Enabled:=true;
break;
end
else
begin
frmPlayBack.StringGrid1.RowCount := frmPlayBack.StringGrid1.RowCount -1;
MessageBox(0,'由于服务器忙,或网络故障,获取文件列表异常终止!','错误',MB_OK+16);
frmPlayBack.cmdSearch.Enabled :=true;
Break;
end;
end;
CloseHandle(m_hFileThread);
m_hFileThread := 0;
NET_DVR_FindClose(m_lFileHandle);
end;
procedure TfrmPlayBack.FormCreate(Sender: TObject);
begin
DateTimePicker1.DateTime := Now;
DateTimePicker2.DateTime := Now;
DateTimePicker3.DateTime := Now;
DateTimePicker4.DateTime := Now;
StringGrid1.Cells[1,0]:='文件名称';
StringGrid1.Cells[2,0]:='文件大小';
StringGrid1.Cells[3,0]:='开始时间';
StringGrid1.Cells[4,0]:='结束时间';
m_PlayIndex :=-1;
m_bStartDownload :=false;
end;
procedure TfrmPlayBack.cmdSearchClick(Sender: TObject);
var
StartTime,StopTime:NET_DVR_TIME;
FileType:Integer;
dwThreadId:DWORD;
begin
DateTimePicker1.Time:=DateTimePicker2.Time;
DateTimePicker3.Time:=DateTimePicker4.Time;
if(DateTimePicker1.DateTime>DateTimePicker3.DateTime) then
begin
MessageBox(Handle,'起始时间不能大于结束时间!','错误',MB_OK+48);
Exit;
end;
StringGrid1.RowCount:=2;
StringGrid1.Cells[1,1]:='';
StringGrid1.Cells[2,1]:='';
StringGrid1.Cells[3,1]:='';
cmdSearch.Enabled:=false;
StartTime.dwYear := YearOf(DateTimePicker1.DateTime); //年
StartTime.dwMonth := MonthOf(DateTimePicker1.DateTime); //月
StartTime.dwDay := DayOf(DateTimePicker1.DateTime); //日
StartTime.dwHour := HourOf(DateTimePicker1.DateTime); //时
StartTime.dwMinute := MinuteOf(DateTimePicker1.DateTime);//分
StartTime.dwSecond := SecondOf(DateTimePicker1.DateTime);//秒
StopTime.dwYear := YearOf(DateTimePicker3.DateTime); //年
StopTime.dwMonth := MonthOf(DateTimePicker3.DateTime); //月
StopTime.dwDay := DayOf(DateTimePicker3.DateTime); //日
StopTime.dwHour := HourOf(DateTimePicker3.DateTime); //时
StopTime.dwMinute := MinuteOf(DateTimePicker3.DateTime);//分
StopTime.dwSecond := SecondOf(DateTimePicker3.DateTime);//秒
if ComboBox2.ItemIndex=ComboBox2.Items.Count-1 then
FileType := $FF
else
FileType := ComboBox2.ItemIndex;
m_lFileHandle := NET_DVR_FindFile(iLoginID,ComboBox1.ItemIndex+1,FileType, @StartTime, @StopTime);
if m_lFileHandle < 0 then
begin
MessageBox(Handle,'获取录像文件名列表失败!','错误',MB_OK+16);
cmdSearch.Enabled:=true;
exit;
end;
if m_hFileThread = 0 then
m_hFileThread:=CreateThread(nil,0,@GetFileThread,nil,0,dwThreadId);
if m_hFileThread=0 then
begin
MessageBox(Handle,'打开线程失败!','错误',MB_OK+16);
cmdSearch.Enabled :=true;
end;
end;
procedure TfrmPlayBack.Button2Click(Sender: TObject);
begin
NET_DVR_FindClose(m_lFileHandle);
end;
procedure TfrmPlayBack.cmdPlayClick(Sender: TObject);
var
FileName:string;
begin
FileName:= StringGrid1.Cells[1,StringGrid1.Row];
if FileName='' then
begin
MessageBox(Handle,'请选择将要播放的文件!','错误',MB_OK+16);
exit;
end;
if m_PlayIndex>=0 then
begin
NET_DVR_StopPlayBack(m_PlayIndex);
m_PlayIndex := -1;
Sleep(400);
end;
m_PlayIndex := NET_DVR_PlayBackByName(iLoginID, PChar(FileName), VideoPanel.Handle);
if m_PlayIndex=-1 then
begin
MessageBox(Handle,'远程播放当前视频文件失败!','错误',MB_OK+16);
exit;
end;
NET_DVR_PlayBackControl(m_PlayIndex, NET_DVR_PLAYSTART, 0, nil);
NET_DVR_PlayBackControl(m_PlayIndex, NET_DVR_PLAYNORMAL, 0, nil); //正常播放
cmdPlay.Enabled:=false;
cmdStop.Enabled:=true
end;
procedure TfrmPlayBack.cmdStopClick(Sender: TObject);
begin
if m_PlayIndex>-1 then
begin
NET_DVR_StopPlayBack(m_PlayIndex);
m_PlayIndex := -1;
VideoPanel.Refresh;
end;
cmdPlay.Enabled:=True;
cmdStop.Enabled:=False;
end;
procedure TfrmPlayBack.cmdDownFileClick(Sender: TObject);
var
sfilename :String;
sLocalFile:String;
begin
if not m_bStartDownload then
begin
if StringGrid1.Cells[1,StringGrid1.Row]='' then
begin
MessageBox(Handle,'请选择将要下载的文件!','错误',MB_OK+16);
exit;
end;
sfilename := StringGrid1.Cells[1,StringGrid1.Row];
m_lFileHandle:=-1;
//判断当前
sLocalFile:=txtFilePath.Text+'\'+sfilename+'.mp4';
if (FileExists(sLocalFile)) then
begin
if(MessageBox(Handle,'本地文件已存在,您确定要覆盖当前文件吗?','下载',MB_YESNO+32)=IDNO) then
begin
exit;
end;
end;
DeleteFile(sLocalFile);
ProgressBar1.Position:=0;
m_lFileHandle := NET_DVR_GetFileByName(iLoginID,PChar(sfilename),PChar(sLocalFile));
if(m_lFileHandle >= 0) then
begin
NET_DVR_PlayBackControl(m_lFileHandle, NET_DVR_PLAYSTART, 0, nil);
cmdDownFile.Caption:='取消';
m_bStartDownload:=true;
Timer1.Enabled:=true;
end
else
begin
m_bStartDownload:=false;
Timer1.Enabled:=false;
MessageBox(Handle,'下载远程录像文件失败!','错误',MB_OK+16);
end;
end
else
begin
//停止下载
NET_DVR_StopGetFile(m_lFileHandle);
cmdDownFile.Caption:='下载';
Timer1.Enabled:=false;
m_bStartDownload:=false;
MessageBox(Handle,'取消下载成功!','成功',MB_OK+64);
ProgressBar1.Position:=0;
m_lFileHandle:=-1;
end;
end;
procedure TfrmPlayBack.Timer1Timer(Sender: TObject);
var
nPos:Integer;
begin
if(m_bStartDownload) then
begin
nPos:=NET_DVR_GetDownloadPos(m_lFileHandle);
if(nPos < 0) then //失败
begin
NET_DVR_StopGetFile(m_lFileHandle);
Timer1.Enabled:=false;
m_bStartDownload:=false;
cmdDownFile.Caption:='开始下载';
ProgressBar1.Position:=0;
m_lFileHandle:=-1;
MessageBox(Handle,'获取下载进度失败!','下载',MB_OK+16);
end;
if(nPos = 100) then //下载结束
begin
NET_DVR_StopGetFile(m_lFileHandle);
Timer1.Enabled:=false;
m_bStartDownload:=false;
cmdDownFile.Caption:='开始下载';
ProgressBar1.Position:=0;
m_lFileHandle:=-1;
MessageBox(Handle,'远程文件下载完成!','下载',MB_OK+64);
end;
if(nPos > 100) then //由于网络原因或DVR忙,下载异常终止
begin
NET_DVR_StopGetFile(m_lFileHandle);
Timer1.Enabled:=false;
m_bStartDownload:=false;
cmdDownFile.Caption:='开始下载';
ProgressBar1.Position:=0;
m_lFileHandle:=-1;
MessageBox(Handle,'由于网络原因或DVR忙,下载异常终止!','下载',MB_OK+16);
end
else
begin
ProgressBar1.Position:=nPos;
end;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -