前言
本該把這部分和上一篇合成一篇的,無奈掙紮半天,還是想對稱起來,用戶端3篇、伺服器端3篇——所有播放器也勉強湊3篇吧(封裝api的文章不算) : )
注意
本系列文章限于學習交流,注重過程,由于涉及公司,是以不提供源代碼下載下傳,非常抱歉!!但是請大家放心,核心、實作以及其他能夠貼出來的代碼我都會貼出來,并且争取盡所能的回答留言裡的每一個問題,感謝大家關注,歡迎交流 :)
系列
正文
一、控制播放
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。
結束
現在在做語音部分,受阻中...
轉載:http://www.cnblogs.com/over140/archive/2009/03/30/1421531.html