www.pudn.com > hmmPlatform.rar > WaveOut.cpp


////////////////////////////////////////////////////////////////////////// 
// class CWaveOut 
// 
// 功能:	语音播放的基类 
// 创建人:	陈文凯 (chwkai@gmail.com) 
// 创建日期:2005年5月19日 
// 修改人: 
// 修改日期: 
// 版本 
 
#include "StdAfx.h" 
#include ".\waveout.h" 
 
////////////////////////////////////////////////////////////////////////// 
// 构造函数 
CWaveOut::CWaveOut(void) 
{ 
	this->m_hWnd = 0; 
	this->m_hWaveOut = 0; 
 
	// 设置标志位 
	this->m_bPlaying = FALSE; 
	this->m_bPaused = FALSE; 
} 
 
////////////////////////////////////////////////////////////////////////// 
// 析构函数 
CWaveOut::~CWaveOut(void) 
{ 
	this->Stop(); 
} 
 
////////////////////////////////////////////////////////////////////////// 
// 打开输出设备,设置回调窗口 
BOOL CWaveOut::Init(WAVEFORMATEX fmt, DWORD hWnd) 
{ 
	MMRESULT nResult = 0; 
	BOOL bRet = false; 
 
	// 检查是否有可用的输出设备 
	// 若设备已经打开,则返回,不进行任何操作 
	if (this->IsDevAvailable()) 
	{ 
		// 设置format 
		this->m_format = fmt; 
		// 设置回调窗口 
		this->m_hWnd = hWnd; 
 
		// 打开输出设备 
		nResult = waveOutOpen(&this->m_hWaveOut, WAVE_MAPPER, &this->m_format,  
				this->m_hWnd, 0, CALLBACK_WINDOW); 
 
		bRet = (nResult == MMSYSERR_NOERROR); 
	} 
 
	return bRet; 
} 
 
////////////////////////////////////////////////////////////////////////// 
// 回收资源 
void CWaveOut::Dispose() 
{ 
	this->m_hWnd = 0; 
	this->m_hWaveOut = 0; 
 
	// 设置标志位 
	this->m_bPlaying = FALSE; 
	this->m_bPaused = FALSE; 
} 
 
////////////////////////////////////////////////////////////////////////// 
// 停止播放 
void CWaveOut::Stop() 
{ 
	// 停止播放 
	waveOutReset(this->m_hWaveOut); 
	 
	// 设定设备和播放状态 
	this->m_bPlaying = FALSE; 
	this->m_bPaused = FALSE; 
} 
 
////////////////////////////////////////////////////////////////////////// 
// 关闭输出设备 
void CWaveOut::CloseDev() 
{ 
	// 关闭输出设备 
	waveOutClose(this->m_hWaveOut); 
} 
 
////////////////////////////////////////////////////////////////////////// 
// 是否存在可用的输出设备 
BOOL CWaveOut::IsDevAvailable() const 
{ 
	return (waveOutGetNumDevs() > 0); 
} 
 
////////////////////////////////////////////////////////////////////////// 
// 修改当前状态为正在播放 
void CWaveOut::Start() 
{ 
	// 标识正在播放 
	this->m_bPlaying = TRUE; 
	this->m_bPaused = FALSE; 
} 
 
////////////////////////////////////////////////////////////////////////// 
// 暂停播放 
void CWaveOut::Pause() 
{ 
	if (this->m_bPlaying) 
	{ 
		waveOutPause(this->m_hWaveOut); 
 
		this->m_bPlaying = FALSE; 
		this->m_bPaused = TRUE; 
	} 
} 
 
////////////////////////////////////////////////////////////////////////// 
// 恢复播放暂停的音频数据 
void CWaveOut::Resume() 
{ 
	if (this->m_bPaused) 
	{ 
		waveOutRestart(this->m_hWaveOut); 
 
		this->m_bPlaying = TRUE; 
		this->m_bPaused = FALSE; 
	} 
}