|
|
发表于 2008-1-14 03:27:00
|
显示全部楼层
Re:如何快速构建c++类?
//instMedia.h
#ifndef INST_MEDIA_H
#define INST_MEDIA_H
#include <instTypes.h>
#include <instDefines.h>
#include <instStr.h>
#include <instProcess.h>
#include <dshow.h>
namespace inst
{
class CMedia
{
private:
IGraphBuilder *m_pGraphBuilder;
IMediaControl *m_pMediaControl;
IMediaPosition *m_pMediaPosition;
IBasicAudio *m_pBasicAudio;
IBasicVideo *m_pBasicVideo;
IVideoWindow *m_pVideoWindow;
Bool m_bPlaying;
Bool m_bLoop;
CTimer *tmrTimer;
void tmrTimer_Tick(CTimer *es)
{
if(!IsOpened())return;
if(!m_bPlaying)return;
if(GetCrrtPos()>=GetDuration())Seek(0);
}
CProcessMgr *m_pProcessMgr;
public:
CMedia(CProcessMgr *processmgr)
{
m_pGraphBuilder=null;
m_pMediaControl=null;
m_pMediaPosition=null;
m_pBasicAudio=null;
m_pBasicVideo=null;
m_pVideoWindow=null;
m_bPlaying=False;
m_bLoop=False;
tmrTimer=new CTimer();
AddCallBack(tmrTimer->Tick,this,CMedia::tmrTimer_Tick);
tmrTimer->SetEnabled(False);
tmrTimer->SetInterval(1000);
m_pProcessMgr=processmgr;
if(m_pProcessMgr)m_pProcessMgr->AddProcess(tmrTimer);
}
~CMedia(){ if(m_pProcessMgr)m_pProcessMgr->RemoveProcess(tmrTimer); SafeDel(tmrTimer); }
Bool IsOpened()
{
return m_pGraphBuilder!=null;
}
Bool IsPlaying()
{
return m_bPlaying;
}
Bool IsLoop()
{
return m_bLoop;
}
void SetLoop(Bool loop)
{
if(!tmrTimer)return;
tmrTimer->SetEnabled(loop);
}
void Open(const CStr &filename)
{
CoInitialize(NULL);
CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC,IID_IGraphBuilder,(void**)&m_pGraphBuilder);
m_pGraphBuilder->QueryInterface(IID_IMediaControl,(void**)&m_pMediaControl);
m_pGraphBuilder->QueryInterface(IID_IMediaPosition,(void**)&m_pMediaPosition);
m_pGraphBuilder->QueryInterface(IID_IBasicAudio,(void**)&m_pBasicAudio);
m_pGraphBuilder->QueryInterface(IID_IBasicVideo,(void**)&m_pBasicVideo);
m_pGraphBuilder->QueryInterface(IID_IVideoWindow,(void**)&m_pVideoWindow);
m_pMediaControl->RenderFile((BSTR)filename.ReadW());
m_bPlaying=False;
}
void Close()
{
m_bPlaying=False;
SafeRelease(m_pVideoWindow);
SafeRelease(m_pBasicVideo);
SafeRelease(m_pBasicAudio);
SafeRelease(m_pMediaPosition);
SafeRelease(m_pMediaControl);
SafeRelease(m_pGraphBuilder);
CoUninitialize();
}
void Play()
{
if(!m_pMediaControl)return;
if(0==(m_pMediaControl->Run()))return;
m_bPlaying=True;
}
void Pause()
{
if(!m_pMediaControl)return;
if(0==(m_pMediaControl-> ause()))return;
m_bPlaying=False;
}
void Stop()
{
if(!m_pMediaControl)return;
if(0==(m_pMediaControl->Stop()))return;
m_bPlaying=False;
}
void Seek(Double pos)
{
if(!m_pMediaPosition)return;
m_pMediaPosition->put_CurrentPosition(pos);
}
Double GetDuration()
{
Double ret=0;
if(!m_pMediaPosition)return 0;
m_pMediaPosition->get_Duration(&ret);
return ret;
}
Double GetCrrtPos()
{
Double ret=0;
if(!m_pMediaPosition)return 0;
m_pMediaPosition->get_CurrentPosition(&ret);
return ret;
}
Double GetRate()
{
Double ret=0;
if(!m_pMediaPosition)return 0;
m_pMediaPosition->get_Rate(&ret);
return ret;
}
void SetRate(Double rate)
{
if(!m_pMediaPosition)return;
m_pMediaPosition->put_Rate(rate);
}
Int32 GetVolume()
{
Int32 ret=0;
if(!m_pBasicAudio)return 0;
m_pBasicAudio->get_Volume(&ret);
return ret;
}
void SetVolume(Int32 vol)
{
if(!m_pBasicAudio)return;
m_pBasicAudio->put_Volume(vol);
}
void GetVideoSize(POS *w,POS *h)
{
if(!m_pBasicVideo)return;
m_pBasicVideo->GetVideoSize(w,h);
}
void GetDestPos(POS *l,POS *t,POS *w,POS *h)
{
if(!m_pBasicVideo)return;
m_pBasicVideo->GetDestinationPosition(l,t,w,h);
}
void SetDestPos(POS l,POS t,POS w,POS h)
{
if(!m_pBasicVideo)return;
m_pBasicVideo->SetDestinationPosition(l,t,w,h);
}
void GetSrcPos(POS *l,POS *t,POS *w,POS *h)
{
if(!m_pBasicVideo)return;
m_pBasicVideo->GetSourcePosition(l,t,w,h);
}
void SetSrcPos(POS l,POS t,POS w,POS h)
{
if(!m_pBasicVideo)return;
m_pBasicVideo->SetSourcePosition(l,t,w,h);
}
Int32 GetVideoWndStyle()
{
Int32 ret=0;
if(!m_pVideoWindow)return 0;
m_pVideoWindow->get_WindowStyle(&ret);
return ret;
}
void SetVideoWndStyle(Int32 style)
{
if(!m_pVideoWindow)return;
m_pVideoWindow->put_WindowStyle(style);
}
HWND GetVideoWndOwner()
{
HWND ret=(HWND)null;
if(!m_pVideoWindow)return (HWND)null;
m_pVideoWindow->get_Owner((OAHWND*)(&ret));
return ret;
}
void SetVideoWndOwner(HWND hwnd)
{
if(!m_pVideoWindow)return;
m_pVideoWindow->put_Owner((OAHWND)hwnd);
}
void GetVideoWndPos(POS *l,POS *t,POS *w,POS *h)
{
if(!m_pVideoWindow)return;
m_pVideoWindow->GetWindowPosition(l,t,w,h);
}
void SetVideoWndPos(POS l,POS t,POS w,POS h)
{
if(!m_pVideoWindow)return;
m_pVideoWindow->SetWindowPosition(l,t,w,h);
}
};
}// end of namespace inst
#endif |
|