www.pudn.com > Game_11.rar > Sound.cpp


#include ".\sound.h" 
 
 
CWaveMidi::CWaveMidi(HWND hwnd) 
{ 
	//strcpy((char*)m_pStrSoundPath,L".\\"); 
	CoInitialize(NULL); 
	//创建加载器 
	CoCreateInstance(CLSID_DirectMusicLoader,NULL,CLSCTX_INPROC, 
		IID_IDirectMusicLoader8,(void**) &m_pLoader); 
 
	//创建演奏器 
	CoCreateInstance(CLSID_DirectMusicPerformance,NULL,CLSCTX_INPROC,IID_IDirectMusicPerformance8, 
		(void**) &m_pPerformance); 
 
	//初始化 
	m_pPerformance->InitAudio(NULL,NULL,hwnd,DMUS_APATH_SHARED_STEREOPLUSREVERB,64, 
		DMUS_AUDIOF_ALL,NULL); 
	 
} 
 
CWaveMidi::~CWaveMidi(void) 
{ 
} 
 
LRESULT CWaveMidi::LoadSound(char* strFileName) 
{ 
	WCHAR wstrFile[MAX_PATH]; 
 
	MultiByteToWideChar(CP_ACP, 0, strFileName, -1, wstrFile, MAX_PATH); 
	//设置搜索路径 
//	m_pLoader->SetSearchDirectory(GUID_DirectMusicAllTypes,m_pStrSoundPath,FALSE); 
	//加载Wav或MIDI文件 
	m_pLoader->LoadObjectFromFile(CLSID_DirectMusicSegment, 
		IID_IDirectMusicSegment,wstrFile,(void**) &m_pSegment);	 
	m_pSegment->Download(m_pPerformance); 
	return S_OK; 
} 
 
bool CWaveMidi::Play(DWORD dwNumOfRepeats) 
{ 
 
	//Set the number of times the sound repeats 
	m_pSegment->SetRepeats(dwNumOfRepeats);		//To loop the sound forever, pass in DMUS_SEG_REPEAT_INFINITE 
 
	//Play the loaded sound 
	m_pPerformance-> 
		PlaySegmentEx(m_pSegment, NULL, NULL, 0, 0, NULL, NULL, NULL); 
 
	return true; 
}