正文
一、控制播放
1.1 暫停/播放/停止
VC++ Code:
////////////////////////////////////////////////////////////////////////////////
//Funtion:Play or change the play speed to normal;
///////////////////////////////////////////////////////////////////////////////
void CPlayerDlg::OnPlay()
{
// TODO: Add your control notification handler code here
Play();
}
void CPlayerDlg::Play()
m_nSpeed=0;
OnThrow0();
//#ifdef _TEST_CALLBACK
if(m_bConvert)
Hik_PlayM4_SetDecCallBack(PORT,DecCBFun);
else
m_pMainMenu->EnableMenuItem(ID_FILE_CLOSE, FALSE);
//#endif
if(m_bPlaying)
{
Hik_PlayM4_Play(PORT,GetDlgItem(IDC_SHOW)->m_hWnd);
}
if(m_bStreamType)
{
::SetFilePointer(m_hStreamFile,m_nHeadSize,0,FILE_BEGIN);
Hik_PlayM4_ResetSourceBuffer(PORT);
SetEvent(m_hEventInput);
}
m_bPlaying = Hik_PlayM4_Play(PORT,GetDlgItem(IDC_SHOW)->m_hWnd);
m_bSound=Hik_PlayM4_PlaySound(PORT);
if(m_bPlaying)
SetTimer(PLAY_TIMER,500,NULL);
SetPlayState();
CString csError;
csError.Format("Play the file faild.(%d)",Hik_PlayM4_GetLastError(PORT));
AfxMessageBox(csError);
//////////////////////////////////////////////////////////////////////////////
//Funtion:pause.
void CPlayerDlg::OnPause()
m_bPause=!m_bPause;
Pause(m_bPause);
void CPlayerDlg::Pause(BOOL bPause)
if(m_bPaused == bPause)
return;
m_bPaused=bPause;
Hik_PlayM4_Pause(PORT,bPause);
TRACE("PAUSE %d\n",m_bPaused);
/////////////////////////////////////////////////////////////////////////////
//Function: Stop
void CPlayerDlg::OnStop()
Stop();
if(m_bConvert)
if(outFile!=NULL)
closeWriffFiles();
if(yuvBuf!=NULL)
{
free(yuvBuf);
yuvBuf=NULL;
}
m_bConvert=0;
//

void CPlayerDlg::Stop()
CButton *pButton;
if(!m_bPlaying)
KillTimer(PLAY_TIMER);
if(Hik_PlayM4_StopSound())
m_bSound=FALSE;
pButton = (CButton *)GetDlgItem(IDC_SOUND);
pButton->SetIcon(m_hSoundStopIcon);
//continue before stop.Add by lgl at 9-19;
m_bPause=FALSE;
//stop
m_bPlaying = !Hik_PlayM4_Stop(PORT);
if(!m_bPlaying)
SetStopState();
ResetEvent(m_hEventInput);
C# Code:
//是否暫停
private bool isPause;
/// <summary>
/// 播放
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPlay_Click(object sender, EventArgs e)
if (!string.IsNullOrEmpty(m_strPlayFileName))
{
//是否暫停->播放
if (isPause)
{
HikPlayer.Hik_PlayM4_Pause(PORT, false);
isPause = false;
}
else
OpenFile();
}
/// 暫停
private void btnPause_Click(object sender, EventArgs e)
HikPlayer.Hik_PlayM4_Pause(PORT, true);
isPause = true;
/// 停止
private void btnStop_Click(object sender, EventArgs e)
HikPlayer.Hik_PlayM4_Stop(PORT);
HikPlayer.Hik_PlayM4_CloseFile(PORT);
HikPlayer.Hik_PlayM4_RealeseDDraw();
pVideo.Invalidate(true);
代碼說明:
2. 注意Hik_PlayM4_Pause的第二個參數用法。
1.2 快進/慢進
//Funtion: Fast
void CPlayerDlg::OnFastForward()
//Throw B-Frame ,improve the performance;
if(Hik_PlayM4_Fast(PORT))
m_nSpeed++;
if(m_nSpeed>0)
OnThrow2();
SetFastForwardState();
}
//Funtion: Slow;
void CPlayerDlg::OnFastBackward()
if(Hik_PlayM4_Slow(PORT))
m_nSpeed--;
if(m_nSpeed<=0)
OnThrow0();
SetFastBackWardState();
C# Code:
int m_nSpeed;
/// 快進
private void btnFastForward_Click(object sender, EventArgs e)
if (HikPlayer.Hik_PlayM4_Fast(PORT))
m_nSpeed++;
if (m_nSpeed > 0)
OnThrow2();
/// 慢放
private void btnFastBackward_Click(object sender, EventArgs e)
//慢速播放
if (HikPlayer.Hik_PlayM4_Slow(PORT))
//timer1.Interval
m_nSpeed--;
if (m_nSpeed <= 0)
OnThrow0();
public void OnThrow0()
HikPlayer.Hik_PlayM4_ThrowBFrameNum(PORT, 0);
public void OnThrow2()
HikPlayer.Hik_PlayM4_ThrowBFrameNum(PORT, 2);
1. 注意關于這兩個函數API的說明:
Hik_PlayM4_Fast:快速播放,每次調用将使目前播放速度加快一倍,最多調用4次;要恢複正常播放調用
現在在做語音部分,受阻中...
Hik_PlayM4_Play(),從目前位置開始正常播放。
Hik_PlayM4_Slow:慢速播放,每次調用将使目前播放速度慢一倍;最多調用4次;要恢複正常播放調用Hik_PlayM4_Play。
1.3 開始/末尾
//Funtion:Locate to the file head.
void CPlayerDlg::OnGotoStart()
if(m_bFileRefCreated)
Hik_PlayM4_SetCurrentFrameNum(PORT,0);
Hik_PlayM4_SetPlayPos(PORT,0);
//Funtion:Locate to the end.
void CPlayerDlg::OnGotoEnd()
//Note: May create many WM_FILE_END message. The best way is to synchronize the option;
int nEndFrame=m_nTotalFrames;
while(!Hik_PlayM4_SetCurrentFrameNum(PORT,nEndFrame--))
//TRACE("FrameNum is :%d\n",nEndFrame);
if(nEndFrame==0)
break;
Hik_PlayM4_SetPlayPos(PORT,1);
/// 開始位置
private void btnGotoStart_Click(object sender, EventArgs e)
HikPlayer.Hik_PlayM4_SetPlayPos(PORT, 0);
/// 末尾位置
private void btnGotoEnd_Click(object sender, EventArgs e)
HikPlayer.Hik_PlayM4_SetPlayPos(PORT, 1);
代碼說明:
1. 注意Hik_PlayM4_SetPlayPos的第二個參數取值範圍是0-1之間,即可以了解0是開始位置,1是結束位置;但是有一點比較奇怪,每次都會延遲3秒,即到末尾後還播放3秒鐘!
二、截圖
//////////////////////////////////////////////////////////////////
//Function:The call back funtion for capture image!
/////////////////////////////////////////////////////////////////
void CALLBACK DisplayCBFun(long nPort,\
char * pBuf,long nSize,\
long nWidth,long nHeight,\
long nStamp,long nType,long nReceaved)
if(!g_bCapPic)
CString csFile;
csFile.Format("capture%02d.bmp",pic);
/* switch(nType)
case T_UYVY:
csFile="uyvy.bmp";
break;
case T_YV12:
csFile="yv12.bmp";
case T_RGB32:
csFile="rgb.bmp";
default:
return ;
}*/
//Note:this funtion is slow,so if you want to save as a .bmp file,don't call!
if(!Hik_PLayM4_ConvertToBmpFile(pBuf,nSize,nWidth,nHeight,nType,csFile.GetBuffer(csFile.GetLength())))
CString csErr;
csErr.Format("Convert to bmp faild(%d).",Hik_PlayM4_GetLastError(nPort));
AfxMessageBox(csErr);
pic++;
g_bCapPic=FALSE;
DisplayCBFun DisCB;
/// 截圖
private void btnCapImage_Click(object sender, EventArgs e)
DisCB = new DisplayCBFun(DisplayCBFun);
HikPlayer.Hik_PlayM4_SetDisplayCallBack(PORT, DisCB);
/// 截圖回調函數
/// <param name="nPort"></param>
/// <param name="pBuf"></param>
/// <param name="nSize"></param>
/// <param name="nWidth"></param>
/// <param name="nHeight"></param>
/// <param name="nStamp"></param>
/// <param name="nType"></param>
/// <param name="nReceaved"></param>
public void DisplayCBFun(int nPort, IntPtr pBuf, int nSize, int nWidth, int nHeight, int nStamp, int nType, int nReceaved)
if (HikPlayer.Hik_PLayM4_ConvertToBmpFile(pBuf, nSize, nWidth, nHeight, nType, string.Format("C:\\capture{0}.bmp", nPort)))
MessageBox.Show("轉換bmp失敗!");
//停止回調
HikPlayer.Hik_PlayM4_SetDisplayCallBack(PORT, null);
代碼說明:
1. 這裡和源代碼有點出入,他用的是g_bCapPic變量來控制是否捕獲圖檔,我用的是設定委托執行個體和null來達到。API說明:設定抓圖回調函數;注意要盡快傳回,如果要停止回調,可以把回調函數指針DisplayCBFun設為NULL。
本文轉自over140 51CTO部落格,原文連結:http://blog.51cto.com/over140/586632,如需轉載請自行聯系原作者