| 
									
										
										
										
											2017-10-20 00:59:26 +08:00
										 |  |  |  | #include "..\etools.h"
 | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | #include "..\Win\winbase.h"
 | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | #include <map>
 | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | #include <mmsystem.h>
 | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | #include <Digitalv.h>
 | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | #pragma comment(lib , "winmm.lib")
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | static size_t Hash(const e2d::EString & key); | 
					
						
							|  |  |  |  | static bool ExtractResource(LPCTSTR strDstFile, LPCTSTR strResType, LPCTSTR strResName); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  |  | // MciPlayer
 | 
					
						
							|  |  |  |  | ////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | class MciPlayer | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | public: | 
					
						
							|  |  |  |  | 	MciPlayer(); | 
					
						
							|  |  |  |  | 	~MciPlayer(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	void close(); | 
					
						
							| 
									
										
										
										
											2017-11-03 22:14:07 +08:00
										 |  |  |  | 	bool open(const e2d::EString & pFileName, size_t uId); | 
					
						
							|  |  |  |  | 	bool open(const e2d::EString & pResouceName, const e2d::EString & pResouceType, const e2d::EString & musicExtension, size_t uId); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 	void play(bool bLoop = false); | 
					
						
							|  |  |  |  | 	void pause(); | 
					
						
							|  |  |  |  | 	void resume(); | 
					
						
							|  |  |  |  | 	void stop(); | 
					
						
							|  |  |  |  | 	void rewind(); | 
					
						
							|  |  |  |  | 	void setVolume(float volume); | 
					
						
							|  |  |  |  | 	bool isPlaying(); | 
					
						
							| 
									
										
										
										
											2017-11-03 22:14:07 +08:00
										 |  |  |  | 	size_t getSoundID(); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | private: | 
					
						
							|  |  |  |  | 	void _sendCommand(int nCommand, DWORD_PTR param1 = 0, DWORD_PTR parma2 = 0); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	MCIDEVICEID m_dev; | 
					
						
							| 
									
										
										
										
											2017-11-03 22:14:07 +08:00
										 |  |  |  | 	size_t		m_nSoundID; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 	bool        m_bPlaying; | 
					
						
							|  |  |  |  | 	bool		m_bLoop; | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | 	e2d::EString m_sTempFileName; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | MciPlayer::MciPlayer() : | 
					
						
							|  |  |  |  | 	m_dev(0L), | 
					
						
							|  |  |  |  | 	m_nSoundID(0), | 
					
						
							|  |  |  |  | 	m_bPlaying(false), | 
					
						
							|  |  |  |  | 	m_bLoop(false) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | MciPlayer::~MciPlayer() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	close();	// <20>رղ<D8B1><D5B2><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-03 22:14:07 +08:00
										 |  |  |  | bool MciPlayer::open(const e2d::EString & pFileName, size_t uId) | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	// <20><><EFBFBD>Բ<EFBFBD><D4B2><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5>ļ<EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | 	if (pFileName.empty())  | 
					
						
							|  |  |  |  | 		return false; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 	// ֹͣ<CDA3><D6B9>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 	close(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// <20><><EFBFBD><EFBFBD> MCI_OPEN_PARMS <20><><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 	MCI_OPEN_PARMS mciOpen = { 0 }; | 
					
						
							|  |  |  |  | 	mciOpen.lpstrElementName = pFileName.c_str(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
 | 
					
						
							|  |  |  |  | 	MCIERROR mciError; | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | 	mciError = mciSendCommand( | 
					
						
							|  |  |  |  | 		0,  | 
					
						
							|  |  |  |  | 		MCI_OPEN,  | 
					
						
							|  |  |  |  | 		MCI_OPEN_ELEMENT | MCI_NOTIFY, | 
					
						
							|  |  |  |  | 		reinterpret_cast<DWORD_PTR>(&mciOpen) | 
					
						
							|  |  |  |  | 	); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (mciError) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		return false; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	else | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		// <20><><EFBFBD><EFBFBD><EFBFBD>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD>Ϣ
 | 
					
						
							|  |  |  |  | 		m_dev = mciOpen.wDeviceID; | 
					
						
							|  |  |  |  | 		m_nSoundID = uId; | 
					
						
							|  |  |  |  | 		m_bPlaying = false; | 
					
						
							|  |  |  |  | 		return true; | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-03 22:14:07 +08:00
										 |  |  |  | bool MciPlayer::open(const e2d::EString & pResouceName, const e2d::EString & pResouceType, const e2d::EString & musicExtension, size_t uId) | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	// <20><><EFBFBD>Բ<EFBFBD><D4B2><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5>ļ<EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | 	if (pResouceName.empty() || pResouceType.empty() || musicExtension.empty()) return false; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | 	// <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD>ļ<EFBFBD>Ŀ¼
 | 
					
						
							|  |  |  |  | 	e2d::EString tempFileName = e2d::EFileUtils::getTempPath(); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | 	tempFileName.append(L"\\"); | 
					
						
							|  |  |  |  | 	tempFileName.append(std::to_wstring(uId)); | 
					
						
							|  |  |  |  | 	tempFileName.append(L"." + musicExtension); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԴΪ<D4B4><CEAA>ʱ<EFBFBD>ļ<EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | 	if (ExtractResource(tempFileName.c_str(), pResouceType.c_str(), pResouceName.c_str())) | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | 		if (open(tempFileName, uId)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			m_sTempFileName = tempFileName; | 
					
						
							|  |  |  |  | 			return true; | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void MciPlayer::play(bool bLoop) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	// <20>豸Ϊ<E8B1B8><CEAA>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD>
 | 
					
						
							|  |  |  |  | 	if (!m_dev) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		return; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	// <20><><EFBFBD>ò<EFBFBD><C3B2>Ų<EFBFBD><C5B2><EFBFBD>
 | 
					
						
							|  |  |  |  | 	MCI_PLAY_PARMS mciPlay = { 0 }; | 
					
						
							|  |  |  |  | 	MCIERROR s_mciError; | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | 	s_mciError = mciSendCommand( | 
					
						
							|  |  |  |  | 		m_dev,  | 
					
						
							|  |  |  |  | 		MCI_PLAY,  | 
					
						
							|  |  |  |  | 		MCI_FROM | MCI_NOTIFY | (bLoop ? MCI_DGV_PLAY_REPEAT : 0), | 
					
						
							|  |  |  |  | 		reinterpret_cast<DWORD_PTR>(&mciPlay) | 
					
						
							|  |  |  |  | 	); | 
					
						
							|  |  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 	// δ<><CEB4><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD> m_bPlaying Ϊ true
 | 
					
						
							|  |  |  |  | 	if (!s_mciError) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		m_bPlaying = true; | 
					
						
							|  |  |  |  | 		m_bLoop = bLoop; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void MciPlayer::close() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 	if (m_bPlaying) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		stop(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	// <20>ر<EFBFBD><D8B1>豸
 | 
					
						
							|  |  |  |  | 	if (m_dev) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		_sendCommand(MCI_CLOSE); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	// ɾ<><C9BE><EFBFBD><EFBFBD>ʱ<EFBFBD>ļ<EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | 	if (!m_sTempFileName.empty()) | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | 		DeleteFile(m_sTempFileName.c_str()); | 
					
						
							|  |  |  |  | 		m_sTempFileName.clear(); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 	// <20>ָ<EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 	m_dev = 0; | 
					
						
							|  |  |  |  | 	m_bPlaying = false; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void MciPlayer::pause() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	// <20><>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 	_sendCommand(MCI_PAUSE); | 
					
						
							|  |  |  |  | 	m_bPlaying = false; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void MciPlayer::resume() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 	_sendCommand(MCI_RESUME); | 
					
						
							|  |  |  |  | 	m_bPlaying = true; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void MciPlayer::stop() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 	_sendCommand(MCI_STOP); | 
					
						
							|  |  |  |  | 	m_bPlaying = false; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void MciPlayer::rewind() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	// <20>豸Ϊ<E8B1B8><CEAA>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD>
 | 
					
						
							|  |  |  |  | 	if (!m_dev) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		return; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	// <20><><EFBFBD>ò<EFBFBD><C3B2><EFBFBD>λ<EFBFBD><CEBB>
 | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | 	mciSendCommand( | 
					
						
							|  |  |  |  | 		m_dev, | 
					
						
							|  |  |  |  | 		MCI_SEEK,  | 
					
						
							|  |  |  |  | 		MCI_SEEK_TO_START | MCI_NOTIFY,  | 
					
						
							|  |  |  |  | 		0 | 
					
						
							|  |  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 	MCI_PLAY_PARMS mciPlay = { 0 }; | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | 	MCIERROR s_mciError; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 	s_mciError = mciSendCommand( | 
					
						
							|  |  |  |  | 		m_dev,  | 
					
						
							|  |  |  |  | 		MCI_PLAY, | 
					
						
							|  |  |  |  | 		MCI_NOTIFY | (m_bLoop ? MCI_DGV_PLAY_REPEAT : 0), | 
					
						
							|  |  |  |  | 		reinterpret_cast<DWORD_PTR>(&mciPlay) | 
					
						
							|  |  |  |  | 	); | 
					
						
							|  |  |  |  | 	m_bPlaying = s_mciError ? false : true; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void MciPlayer::setVolume(float volume) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	MCI_DGV_SETAUDIO_PARMS  mciSetAudioPara = { 0 }; | 
					
						
							|  |  |  |  | 	mciSetAudioPara.dwItem = MCI_DGV_SETAUDIO_VOLUME; | 
					
						
							|  |  |  |  | 	mciSetAudioPara.dwValue = DWORD(1000 * min(max(volume, 0), 1)); | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | 	mciSendCommand( | 
					
						
							|  |  |  |  | 		m_dev,  | 
					
						
							|  |  |  |  | 		MCI_SETAUDIO,  | 
					
						
							|  |  |  |  | 		MCI_NOTIFY | MCI_DGV_SETAUDIO_VALUE | MCI_DGV_SETAUDIO_ITEM, | 
					
						
							|  |  |  |  | 		(DWORD_PTR)&mciSetAudioPara | 
					
						
							|  |  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | bool MciPlayer::isPlaying() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	return m_bPlaying; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-03 22:14:07 +08:00
										 |  |  |  | size_t MciPlayer::getSoundID() | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	return m_nSoundID; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void MciPlayer::_sendCommand(int nCommand, DWORD_PTR param1, DWORD_PTR parma2) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	// <20><><EFBFBD>豸ʱ<E8B1B8><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD>
 | 
					
						
							|  |  |  |  | 	if (!m_dev) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		return; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	// <20><><EFBFBD><EFBFBD>ǰ<EFBFBD>豸<EFBFBD><E8B1B8><EFBFBD>Ͳ<EFBFBD><CDB2><EFBFBD>
 | 
					
						
							|  |  |  |  | 	mciSendCommand(m_dev, nCommand, param1, parma2); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  |  | // e2d::EMusicUtils
 | 
					
						
							|  |  |  |  | ////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-03 22:14:07 +08:00
										 |  |  |  | typedef std::map<size_t, MciPlayer *> MusicList; | 
					
						
							|  |  |  |  | typedef std::pair<size_t, MciPlayer *> Music; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | static MusicList& getMciPlayerList() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	static MusicList s_List; | 
					
						
							|  |  |  |  | 	return s_List; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | static MciPlayer& getBgMciPlayer() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	static MciPlayer s_Music; | 
					
						
							|  |  |  |  | 	return s_Music; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::end() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 	getBgMciPlayer().close(); | 
					
						
							|  |  |  |  | 	// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 	for (auto& iter : getMciPlayerList()) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		SafeDelete(&iter.second); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>
 | 
					
						
							|  |  |  |  | 	getMciPlayerList().clear(); | 
					
						
							|  |  |  |  | 	return; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::setVolume(float volume) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	// <20><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 	getBgMciPlayer().setVolume(volume); | 
					
						
							|  |  |  |  | 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 	for (auto& iter : getMciPlayerList()) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		iter.second->setVolume(volume); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::setVolume(const EString & musicFilePath, float volume) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-11-03 22:14:07 +08:00
										 |  |  |  | 	size_t nRet = ::Hash(musicFilePath); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	MusicList::iterator p = getMciPlayerList().find(nRet); | 
					
						
							|  |  |  |  | 	if (p != getMciPlayerList().end()) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		p->second->setVolume(volume); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::playBackgroundMusic(const EString & musicFilePath, bool bLoop) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (musicFilePath.empty()) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		return; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	getBgMciPlayer().open(musicFilePath, ::Hash(musicFilePath)); | 
					
						
							|  |  |  |  | 	getBgMciPlayer().play(bLoop); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::playBackgroundMusic(const EString & musicResourceName, const EString & musicResourceType, const EString & musicExtension, bool loop) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (musicResourceName.empty() || musicResourceType.empty()) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		return; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	getBgMciPlayer().open(musicResourceName, musicResourceType, musicExtension, ::Hash(musicResourceName)); | 
					
						
							|  |  |  |  | 	getBgMciPlayer().play(loop); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::stopBackgroundMusic(bool bReleaseData) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (bReleaseData) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		getBgMciPlayer().close(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	else | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		getBgMciPlayer().stop(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::pauseBackgroundMusic() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	getBgMciPlayer().pause(); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::resumeBackgroundMusic() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	getBgMciPlayer().resume(); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::rewindBackgroundMusic() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	getBgMciPlayer().rewind(); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | bool e2d::EMusicUtils::isBackgroundMusicPlaying() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	return getBgMciPlayer().isPlaying(); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::setBackgroundMusicVolume(float volume) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	getBgMciPlayer().setVolume(volume); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::playMusic(const EString & musicFilePath, bool bLoop) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-04 10:03:14 +08:00
										 |  |  |  | 	MCI_OPEN_PARMS mciOpen = { 0 }; | 
					
						
							|  |  |  |  | 	mciOpen.lpstrElementName = musicFilePath.c_str(); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-04 10:03:14 +08:00
										 |  |  |  | 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
 | 
					
						
							|  |  |  |  | 	MCIERROR mciError; | 
					
						
							|  |  |  |  | 	mciError = mciSendCommand( | 
					
						
							|  |  |  |  | 		0, | 
					
						
							|  |  |  |  | 		MCI_OPEN, | 
					
						
							|  |  |  |  | 		MCI_OPEN_ELEMENT | MCI_NOTIFY, | 
					
						
							|  |  |  |  | 		reinterpret_cast<DWORD_PTR>(&mciOpen) | 
					
						
							|  |  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-04 10:03:14 +08:00
										 |  |  |  | 	if (!mciError) | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-12-04 10:03:14 +08:00
										 |  |  |  | 		// <20><><EFBFBD>ò<EFBFBD><C3B2>Ų<EFBFBD><C5B2><EFBFBD>
 | 
					
						
							|  |  |  |  | 		MCI_PLAY_PARMS mciPlay = { 0 }; | 
					
						
							|  |  |  |  | 		MCIERROR s_mciError; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 		s_mciError = mciSendCommand( | 
					
						
							|  |  |  |  | 			mciOpen.wDeviceID, | 
					
						
							|  |  |  |  | 			MCI_PLAY, | 
					
						
							|  |  |  |  | 			MCI_FROM | MCI_NOTIFY | (bLoop ? MCI_DGV_PLAY_REPEAT : 0), | 
					
						
							|  |  |  |  | 			reinterpret_cast<DWORD_PTR>(&mciPlay) | 
					
						
							|  |  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::playMusic(const EString & musicResourceName, const EString & musicResourceType, const EString & musicExtension, bool loop) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-11-03 22:14:07 +08:00
										 |  |  |  | 	size_t nRet = ::Hash(musicResourceName); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	preloadMusic(musicResourceName, musicResourceType, musicExtension); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	MusicList::iterator p = getMciPlayerList().find(nRet); | 
					
						
							|  |  |  |  | 	if (p != getMciPlayerList().end()) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		p->second->play(loop); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::_stopMusic(size_t nSoundId) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	MusicList::iterator p = getMciPlayerList().find(nSoundId); | 
					
						
							|  |  |  |  | 	if (p != getMciPlayerList().end()) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		p->second->stop(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::preloadMusic(const EString & musicFilePath) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (musicFilePath.empty()) return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-03 22:14:07 +08:00
										 |  |  |  | 	size_t nRet = ::Hash(musicFilePath); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (getMciPlayerList().end() != getMciPlayerList().find(nRet)) return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	getMciPlayerList().insert(Music(nRet, new MciPlayer())); | 
					
						
							|  |  |  |  | 	MciPlayer * pPlayer = getMciPlayerList()[nRet]; | 
					
						
							|  |  |  |  | 	pPlayer->open(musicFilePath, nRet); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (nRet == pPlayer->getSoundID()) return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	delete pPlayer; | 
					
						
							|  |  |  |  | 	getMciPlayerList().erase(nRet); | 
					
						
							|  |  |  |  | 	nRet = 0; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::preloadMusic(const EString & musicResourceName, const EString & musicResourceType, const EString & musicExtension) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (musicResourceName.empty() || musicResourceType.empty()) return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-03 22:14:07 +08:00
										 |  |  |  | 	size_t nRet = ::Hash(musicResourceName); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (getMciPlayerList().end() != getMciPlayerList().find(nRet)) return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	getMciPlayerList().insert(Music(nRet, new MciPlayer())); | 
					
						
							|  |  |  |  | 	MciPlayer * pPlayer = getMciPlayerList()[nRet]; | 
					
						
							|  |  |  |  | 	pPlayer->open(musicResourceName, musicResourceType, musicExtension, nRet); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (nRet == pPlayer->getSoundID()) return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	delete pPlayer; | 
					
						
							|  |  |  |  | 	getMciPlayerList().erase(nRet); | 
					
						
							|  |  |  |  | 	nRet = 0; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::_pauseMusic(size_t nSoundId) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	MusicList::iterator p = getMciPlayerList().find(nSoundId); | 
					
						
							|  |  |  |  | 	if (p != getMciPlayerList().end()) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		p->second->pause(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::pauseAllMusics() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	for (auto& iter : getMciPlayerList()) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		iter.second->pause(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::_resumeMusic(size_t nSoundId) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	MusicList::iterator p = getMciPlayerList().find(nSoundId); | 
					
						
							|  |  |  |  | 	if (p != getMciPlayerList().end()) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		p->second->resume(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::resumeAllMusics() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	for (auto& iter : getMciPlayerList()) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		iter.second->resume(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::stopAllMusics() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	for (auto& iter : getMciPlayerList()) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		iter.second->stop(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::EMusicUtils::unloadMusic(const EString & musicFilePath) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-11-03 22:14:07 +08:00
										 |  |  |  | 	size_t nID = ::Hash(musicFilePath); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	MusicList::iterator p = getMciPlayerList().find(nID); | 
					
						
							|  |  |  |  | 	if (p != getMciPlayerList().end()) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		SafeDelete(&p->second); | 
					
						
							|  |  |  |  | 		getMciPlayerList().erase(nID); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | size_t Hash(const e2d::EString & key) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	static std::hash<e2d::EString> h; | 
					
						
							|  |  |  |  | 	return h(key); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | bool ExtractResource(LPCTSTR strDstFile, LPCTSTR strResType, LPCTSTR strResName) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
 | 
					
						
							|  |  |  |  | 	HANDLE hFile = ::CreateFile(strDstFile, GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL); | 
					
						
							|  |  |  |  | 	if (hFile == INVALID_HANDLE_VALUE) | 
					
						
							|  |  |  |  | 		return false; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>ļ<EFBFBD><C4BC>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD>ڴ桢<DAB4>õ<EFBFBD><C3B5><EFBFBD>Դ<EFBFBD><D4B4>С
 | 
					
						
							|  |  |  |  | 	HRSRC	hRes = ::FindResource(NULL, strResName, strResType); | 
					
						
							|  |  |  |  | 	HGLOBAL	hMem = ::LoadResource(NULL, hRes); | 
					
						
							|  |  |  |  | 	DWORD	dwSize = ::SizeofResource(NULL, hRes); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// д<><D0B4><EFBFBD>ļ<EFBFBD>
 | 
					
						
							|  |  |  |  | 	DWORD dwWrite = 0;  	// <20><><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>ֽ<EFBFBD>
 | 
					
						
							|  |  |  |  | 	::WriteFile(hFile, hMem, dwSize, &dwWrite, NULL); | 
					
						
							|  |  |  |  | 	::CloseHandle(hFile); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	return true; | 
					
						
							|  |  |  |  | } |