您的位置:首页 > 其它

多媒体定时器(回调函数定时方式)控制类

2005-04-02 12:09 225 查看
//////////////////////////////////////////////////////////////////////
// MultimediaTimer.h: interface for the CMultimediaTimer class.
// 2005.3.9
// If this code works, it was written by TY.
// If not, I don't know who wrote it.
//
// Use this CMultimediaTimer class as the fellows:
// step 1 : create a CMultimediaTimer object
// step 2 : define the Timer Callback Proc function in your programe,
// the function must be the same type as below:
// void PASCAL TimerProc(UINT wTimerID, UINT msg,DWORD dwUser,DWORD dwl,DWORD dw2);
// step 3 : use class member function SetProcParameter, set TimerProc and Parameter,
// if you need you can also use SetRange & SetCurRes functions to Modify
// CMultimediaTimer object initial state in this step
// step 4 : use class play control function as you need
// Welcome any advice wangjianddy@gmail.com
//////////////////////////////////////////////////////////////////////
#if !defined(TY_MultimediaTimer)
#define TY_MultimediaTimer
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "Mmsystem.h" //媒体定时器
#pragma comment(lib,"winmm")
const UINT TIMER_ACCURACY = 1; //定义时钟分辨率,以ms为单位
#define MTIMER_MAXLIMIT 0x0001
#define MTIMER_MINLIMIT 0x0002
#define MTIMER_NORMAL 0x0004
#define MCONTROL_NOSPEEDUP MTIMER_MINLIMIT
#define MCONTROL_NOSLOWDOWN MTIMER_MAXLIMIT
#define MCONTROL_NOLIMIT MTIMER_NORMAL
class CMultimediaTimer
{
public:
CMultimediaTimer();
virtual ~CMultimediaTimer();
public://播放控制函数
void Start(UINT timerCurRes, BOOL bOneShot); //启动定时器
void Start(BOOL bOneShot = FALSE);
void Stop(); //停止定时器
void Pause(); //暂停定时器
BOOL SpeedUp(); //加快播放速度
BOOL SlowDown(); //减慢播放速度
void SetRange(UINT Lower, UINT Upper); //设置定时时间调节范围,对带参的Start和SetCurRes函数无效
void GetRange(UINT &Lower, UINT &Upper) const; //获得定时时间范围
void SetCurRes(UINT timerCurRes); //获得当前时间间隔
void SetProcParameter(LPTIMECALLBACK callbackproc, DWORD dwUser);
//设置回调函数和传入参数
BOOL IsStart() const; //定时器是否运行
BOOL IsStop() const; //定时器是否终止
BOOL IsPause() const; //定时器是否暂停
UINT GetResState(); //获得定时器时间间隔状态
UINT GetCurRes() const; //获得当前时间间隔
const TIMECAPS& timeGetDevCaps() const; //获得定时器分辨率范围
protected:
virtual void OnSpeedUp(UINT &timerCurRes); //当速度提高的时候被调用
virtual void OnSlowDown(UINT &timerCurRes); //当速度降低的时候被调用
UINT GetResState(UINT Res) const; //返回Res的状态
private:
TIMECAPS m_tc;
UINT m_CurTimerRes; //当前时间间隔
BOOL m_bTimerStop; //定时器是否已经启动
BOOL m_bTimerPause; //定时器是否暂停
DWORD m_dwUser; //回调函数的传入参数
UINT m_wAccuracy; //使用的分辨率
UINT m_TimerID; //定义定时器句柄
LPTIMECALLBACK m_pCallBackProc; //回调函数指针
UINT m_MaxTimeSpace; //最大定时器间隔
UINT m_MinTimeSpace; //最小定时器间隔
};
#endif // !defined(TY_MultimediaTimer)

// MultimediaTimer.cpp: implementation of the CMultimediaTimer class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MultimediaTimer.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMultimediaTimer::CMultimediaTimer()
{
m_MaxTimeSpace = 2560; //最大定时器间隔为 2560 ms [2^X]
m_MinTimeSpace = 10 ; //最小定时器间隔为 10 ms [原速]
//
m_CurTimerRes = 10; //初始多媒体定时间隔10ms
m_bTimerStop = TRUE;
m_bTimerPause = TRUE;
m_pCallBackProc = NULL;
if(::timeGetDevCaps(&m_tc,sizeof(TIMECAPS)) == TIMERR_NOERROR)
{
m_wAccuracy = min(max(m_tc.wPeriodMin,TIMER_ACCURACY),m_tc.wPeriodMax);
timeBeginPeriod(m_wAccuracy);
}
}
CMultimediaTimer::~CMultimediaTimer()
{
timeKillEvent(m_TimerID);
timeEndPeriod(m_wAccuracy);
}
//启动定时器
void CMultimediaTimer::Start(BOOL bOneShot /*= FALSE/**/)
{
ASSERT(m_pCallBackProc != NULL);
UINT fuEvent = (bOneShot ? TIME_ONESHOT : TIME_PERIODIC);
if(m_bTimerStop || m_bTimerPause)
{
m_TimerID = timeSetEvent(m_CurTimerRes,
m_wAccuracy,
(LPTIMECALLBACK) m_pCallBackProc,
m_dwUser,
fuEvent);
m_bTimerStop = FALSE;
m_bTimerPause = FALSE;
}
}
void CMultimediaTimer::Start(UINT timerCurRes, BOOL bOneShot)
{
ASSERT(m_pCallBackProc != NULL);
m_CurTimerRes = max(min(timerCurRes,m_tc.wPeriodMax),m_wAccuracy);
UINT fuEvent = (bOneShot ? TIME_ONESHOT : TIME_PERIODIC);
if(m_bTimerStop || m_bTimerPause)
{
m_TimerID = timeSetEvent(m_CurTimerRes,
m_wAccuracy,
m_pCallBackProc,
m_dwUser,
fuEvent);
m_bTimerStop = FALSE;
m_bTimerPause = FALSE;
}
}
//停止定时器
void CMultimediaTimer::Stop()
{
timeKillEvent(m_TimerID);
m_bTimerStop = TRUE;
m_bTimerPause = FALSE;
}
//暂停定时器
void CMultimediaTimer::Pause()
{
timeKillEvent(m_TimerID);
if(!m_bTimerStop && !m_bTimerPause)
{
m_bTimerPause = TRUE;
}
}
//加快播放速度,减少定时间隔
BOOL CMultimediaTimer::SpeedUp()
{
switch(GetResState())
{
case MTIMER_MAXLIMIT:
m_CurTimerRes = m_MaxTimeSpace;
break;
case MTIMER_MINLIMIT:
m_CurTimerRes = m_MinTimeSpace;
break;
default:;
}
UINT nOldRes = m_CurTimerRes;
OnSpeedUp(nOldRes);
if(GetResState(nOldRes) == MTIMER_NORMAL)
m_CurTimerRes = nOldRes;
else return FALSE;

if(!m_bTimerStop && !m_bTimerPause)
{
timeKillEvent(m_TimerID);
m_TimerID = timeSetEvent(m_CurTimerRes,
m_wAccuracy,
m_pCallBackProc,
m_dwUser,
TIME_PERIODIC);
}
return TRUE;
}
//减慢播放速度,增加定时间隔
BOOL CMultimediaTimer::SlowDown()
{
switch(GetResState())
{
case MTIMER_MAXLIMIT:
m_CurTimerRes = m_MaxTimeSpace;
break;
case MTIMER_MINLIMIT:
m_CurTimerRes = m_MinTimeSpace;
break;
default:;
}
UINT nOldRes = m_CurTimerRes;
OnSlowDown(nOldRes);
if(GetResState(nOldRes) == MTIMER_NORMAL)
m_CurTimerRes = nOldRes;
else return FALSE;

if(!m_bTimerStop && !m_bTimerPause)
{
timeKillEvent(m_TimerID);
m_TimerID = timeSetEvent(m_CurTimerRes,
m_wAccuracy,
m_pCallBackProc,
m_dwUser,
TIME_PERIODIC);
}
return TRUE;
}
//设置定时时间调节范围,对带参的Start和SetCurRes函数无效
void CMultimediaTimer::SetRange(UINT Lower, UINT Upper)
{
//Make change
if(Lower > Upper)
{
UINT temp = Lower;
Lower = Upper;
Upper = temp;
}
m_MinTimeSpace = max(min(Lower,m_tc.wPeriodMax),m_wAccuracy);
m_MaxTimeSpace = max(min(m_tc.wPeriodMax,Upper),m_MinTimeSpace);
}
//获得定时时间范围
void CMultimediaTimer::GetRange(UINT &Lower, UINT &Upper) const
{
Lower = m_MinTimeSpace;
Upper = m_MaxTimeSpace;
}
//获得当前时间间隔
void CMultimediaTimer::SetCurRes(UINT timerCurRes)
{
m_CurTimerRes = max(min(timerCurRes,m_tc.wPeriodMax),m_wAccuracy);
}
//设置回调函数和传入参数
void CMultimediaTimer::SetProcParameter(LPTIMECALLBACK callbackproc, DWORD dwUser)
{
m_pCallBackProc = callbackproc;
m_dwUser = dwUser;
}
//定时器是否运行
BOOL CMultimediaTimer::IsStart() const
{
return (!m_bTimerPause && !m_bTimerStop);
}
//定时器是否已经启动
BOOL CMultimediaTimer::IsStop() const
{
return m_bTimerStop;
}
//获得定时器暂停状态
BOOL CMultimediaTimer::IsPause() const
{
return m_bTimerPause;
}
//返回Res的状态
UINT CMultimediaTimer::GetResState(UINT Res) const
{
if(Res < m_MinTimeSpace)
return MTIMER_MINLIMIT;
if(Res > m_MaxTimeSpace)
return MTIMER_MAXLIMIT;
return MTIMER_NORMAL;
}
//获得定时器状态
UINT CMultimediaTimer::GetResState()
{
UINT temp,r1,r2;
temp = m_CurTimerRes;
OnSpeedUp(temp);
r1 = GetResState(temp);
temp = m_CurTimerRes;
OnSlowDown(temp);
r2 = GetResState(temp);
if(r1 == MTIMER_MINLIMIT && r2 == MTIMER_MAXLIMIT)
return MTIMER_MINLIMIT|MTIMER_MAXLIMIT;
if(r1 == MTIMER_MINLIMIT)
return MTIMER_MINLIMIT;
if(r2 == MTIMER_MAXLIMIT)
return MTIMER_MAXLIMIT;
return MTIMER_NORMAL;
}
//获得当前时间间隔
UINT CMultimediaTimer::GetCurRes() const
{
return m_CurTimerRes;
}
//获得定时器分辨率范围
const TIMECAPS& CMultimediaTimer::timeGetDevCaps() const
{
return m_tc;
}
//当速度提高的时候被调用
void CMultimediaTimer::OnSpeedUp(UINT &timerCurRes)
{//SpeedUp algorithm
timerCurRes = timerCurRes / 2;
}
//当速度降低的时候被调用
void CMultimediaTimer::OnSlowDown(UINT &timerCurRes)
{//SlowDown algorithm
timerCurRes = timerCurRes * 2;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: