417 lines
		
	
	
		
			7.8 KiB
		
	
	
	
		
			C++
		
	
	
	
		
		
			
		
	
	
			417 lines
		
	
	
		
			7.8 KiB
		
	
	
	
		
			C++
		
	
	
	
|  | #include "..\easy2d.h"
 | |||
|  | #include <mmsystem.h>
 | |||
|  | #pragma comment(lib , "winmm.lib")
 | |||
|  | 
 | |||
|  | #include <Digitalv.h>
 | |||
|  | #include <map>
 | |||
|  | 
 | |||
|  | ////////////////////////////////////////////////////////////////////
 | |||
|  | // MciPlayer
 | |||
|  | ////////////////////////////////////////////////////////////////////
 | |||
|  | 
 | |||
|  | class MciPlayer | |||
|  | { | |||
|  | public: | |||
|  | 	MciPlayer(); | |||
|  | 	~MciPlayer(); | |||
|  | 
 | |||
|  | 	void close(); | |||
|  | 	void open(TString pFileName, UINT uId); | |||
|  | 	void play(bool bLoop = false); | |||
|  | 	void pause(); | |||
|  | 	void resume(); | |||
|  | 	void stop(); | |||
|  | 	void rewind(); | |||
|  | 	void setVolume(float volume); | |||
|  | 	bool isPlaying(); | |||
|  | 	UINT getSoundID(); | |||
|  | 
 | |||
|  | private: | |||
|  | 	void _sendCommand(int nCommand, DWORD_PTR param1 = 0, DWORD_PTR parma2 = 0); | |||
|  | 
 | |||
|  | 	MCIDEVICEID m_dev; | |||
|  | 	UINT        m_nSoundID; | |||
|  | 	bool        m_bPlaying; | |||
|  | 	bool		m_bLoop; | |||
|  | 	TString		m_sExt; | |||
|  | }; | |||
|  | 
 | |||
|  | 
 | |||
|  | MciPlayer::MciPlayer() : | |||
|  | 	m_dev(0L), | |||
|  | 	m_nSoundID(0), | |||
|  | 	m_bPlaying(false), | |||
|  | 	m_bLoop(false), | |||
|  | 	m_sExt(_T("")) | |||
|  | { | |||
|  | } | |||
|  | 
 | |||
|  | MciPlayer::~MciPlayer() | |||
|  | { | |||
|  | 	close();	// <20>رղ<D8B1><D5B2><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | } | |||
|  | 
 | |||
|  | void MciPlayer::open(TString pFileName, UINT uId) | |||
|  | { | |||
|  | 	// <20><><EFBFBD>Բ<EFBFBD><D4B2><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5>ļ<EFBFBD>
 | |||
|  | 	if (pFileName.empty() || !PathFileExists(pFileName.c_str())) return; | |||
|  | 	// <20><>ȡ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><D7BA>
 | |||
|  | 	m_sExt = FileUtils::getFileExtension(pFileName); | |||
|  | 	// ֹͣ<CDA3><D6B9>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
 | |||
|  | 	close(); | |||
|  | 
 | |||
|  | 	// <20><><EFBFBD><EFBFBD> MCI_OPEN_PARMS <20><><EFBFBD><EFBFBD>
 | |||
|  | 	MCI_OPEN_PARMS mciOpen = { 0 }; | |||
|  | 	mciOpen.lpstrDeviceType = (LPCTSTR)-1;	// device ID for "all devices"
 | |||
|  | 	mciOpen.lpstrElementName = pFileName.c_str(); | |||
|  | 
 | |||
|  | 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
 | |||
|  | 	MCIERROR mciError; | |||
|  | 	mciError = mciSendCommand(0, MCI_OPEN, MCI_OPEN_ELEMENT, reinterpret_cast<DWORD_PTR>(&mciOpen)); | |||
|  | 	// <20><><EFBFBD>ִ<EFBFBD><D6B4><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD>
 | |||
|  | 	if (mciError) return; | |||
|  | 
 | |||
|  | 	// <20><><EFBFBD><EFBFBD><EFBFBD>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD>Ϣ
 | |||
|  | 	m_dev = mciOpen.wDeviceID; | |||
|  | 	m_nSoundID = uId; | |||
|  | 	m_bPlaying = false; | |||
|  | } | |||
|  | 
 | |||
|  | 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; | |||
|  | 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 	s_mciError = mciSendCommand(m_dev, MCI_PLAY, MCI_FROM | (bLoop ? MCI_DGV_PLAY_REPEAT : 0), reinterpret_cast<DWORD_PTR>(&mciPlay)); | |||
|  | 	// δ<><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); | |||
|  | 	} | |||
|  | 	// <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>
 | |||
|  | 	if (m_sExt == _T(".mid")) | |||
|  | 	{ | |||
|  | 		// midi <20><>֧<EFBFBD><D6A7> MCI_RESUME <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧʹ<D3A6><CAB9> MCI_FROM <20><><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD>ʼλ<CABC><CEBB>
 | |||
|  | 		// <20><>ȡ MCI ״̬
 | |||
|  | 		MCI_STATUS_PARMS mciStatusParms; | |||
|  | 		mciStatusParms.dwItem = MCI_STATUS_POSITION; | |||
|  | 		_sendCommand(MCI_STATUS, MCI_STATUS_ITEM, reinterpret_cast<DWORD_PTR>(&mciStatusParms)); | |||
|  | 		// <20><><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD>ʼλ<CABC>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
 | |||
|  | 		MCI_PLAY_PARMS mciPlayParms; | |||
|  | 		mciPlayParms.dwFrom = (DWORD)mciStatusParms.dwReturn; | |||
|  | 		_sendCommand(MCI_PLAY, MCI_FROM, reinterpret_cast<DWORD_PTR>(&mciPlayParms)); | |||
|  | 	} | |||
|  | 	else | |||
|  | 	{ | |||
|  | 		// <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>
 | |||
|  | 	mciSendCommand(m_dev, MCI_SEEK, MCI_SEEK_TO_START, 0); | |||
|  | 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 	MCI_PLAY_PARMS mciPlay = { 0 }; | |||
|  | 	m_bPlaying = mciSendCommand(m_dev, MCI_PLAY, (m_bLoop ? MCI_DGV_PLAY_REPEAT : 0), reinterpret_cast<DWORD_PTR>(&mciPlay)) ? false : true; | |||
|  | } | |||
|  | 
 | |||
|  | void MciPlayer::setVolume(float volume) | |||
|  | { | |||
|  | 	volume = min(max(volume, 0), 1); | |||
|  | 	MCI_DGV_SETAUDIO_PARMS  mciSetAudioPara = { 0 }; | |||
|  | 	mciSetAudioPara.dwItem = MCI_DGV_SETAUDIO_VOLUME; | |||
|  | 	mciSetAudioPara.dwValue = DWORD(1000 * volume); | |||
|  | 	mciSendCommand(m_dev, MCI_SETAUDIO, MCI_DGV_SETAUDIO_VALUE | MCI_DGV_SETAUDIO_ITEM, (DWORD_PTR)&mciSetAudioPara); | |||
|  | } | |||
|  | 
 | |||
|  | bool MciPlayer::isPlaying() | |||
|  | { | |||
|  | 	return m_bPlaying; | |||
|  | } | |||
|  | 
 | |||
|  | UINT MciPlayer::getSoundID() | |||
|  | { | |||
|  | 	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); | |||
|  | } | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | ////////////////////////////////////////////////////////////////////
 | |||
|  | // MusicUtils
 | |||
|  | ////////////////////////////////////////////////////////////////////
 | |||
|  | 
 | |||
|  | 
 | |||
|  | typedef std::map<unsigned int, MciPlayer *> MusicList; | |||
|  | typedef std::pair<unsigned int, MciPlayer *> Music; | |||
|  | 
 | |||
|  | static unsigned int _Hash(TString key); | |||
|  | 
 | |||
|  | 
 | |||
|  | static MusicList& getMciPlayerList() | |||
|  | { | |||
|  | 	static MusicList s_List; | |||
|  | 	return s_List; | |||
|  | } | |||
|  | 
 | |||
|  | static MciPlayer& getBgMciPlayer() | |||
|  | { | |||
|  | 	static MciPlayer s_Music; | |||
|  | 	return s_Music; | |||
|  | } | |||
|  | 
 | |||
|  | void MusicUtils::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 MusicUtils::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 MusicUtils::setVolume(TString pszFilePath, float volume) | |||
|  | { | |||
|  | 	unsigned int nRet = ::_Hash(pszFilePath); | |||
|  | 
 | |||
|  | 	MusicList::iterator p = getMciPlayerList().find(nRet); | |||
|  | 	if (p != getMciPlayerList().end()) | |||
|  | 	{ | |||
|  | 		p->second->setVolume(volume); | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | void MusicUtils::playBackgroundMusic(TString pszFilePath, bool bLoop) | |||
|  | { | |||
|  | 	if (pszFilePath.empty()) | |||
|  | 	{ | |||
|  | 		return; | |||
|  | 	} | |||
|  | 
 | |||
|  | 	getBgMciPlayer().open(pszFilePath, ::_Hash(pszFilePath)); | |||
|  | 	getBgMciPlayer().play(bLoop); | |||
|  | } | |||
|  | 
 | |||
|  | void MusicUtils::stopBackgroundMusic(bool bReleaseData) | |||
|  | { | |||
|  | 	if (bReleaseData) | |||
|  | 	{ | |||
|  | 		getBgMciPlayer().close(); | |||
|  | 	} | |||
|  | 	else | |||
|  | 	{ | |||
|  | 		getBgMciPlayer().stop(); | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | void MusicUtils::pauseBackgroundMusic() | |||
|  | { | |||
|  | 	getBgMciPlayer().pause(); | |||
|  | } | |||
|  | 
 | |||
|  | void MusicUtils::resumeBackgroundMusic() | |||
|  | { | |||
|  | 	getBgMciPlayer().resume(); | |||
|  | } | |||
|  | 
 | |||
|  | void MusicUtils::rewindBackgroundMusic() | |||
|  | { | |||
|  | 	getBgMciPlayer().rewind(); | |||
|  | } | |||
|  | 
 | |||
|  | bool MusicUtils::isBackgroundMusicPlaying() | |||
|  | { | |||
|  | 	return getBgMciPlayer().isPlaying(); | |||
|  | } | |||
|  | 
 | |||
|  | void MusicUtils::setBackgroundMusicVolume(float volume) | |||
|  | { | |||
|  | 	getBgMciPlayer().setVolume(volume); | |||
|  | } | |||
|  | 
 | |||
|  | unsigned int MusicUtils::playMusic(TString pszFilePath, bool bLoop) | |||
|  | { | |||
|  | 	unsigned int nRet = ::_Hash(pszFilePath); | |||
|  | 
 | |||
|  | 	preloadMusic(pszFilePath); | |||
|  | 
 | |||
|  | 	MusicList::iterator p = getMciPlayerList().find(nRet); | |||
|  | 	if (p != getMciPlayerList().end()) | |||
|  | 	{ | |||
|  | 		p->second->play(bLoop); | |||
|  | 	} | |||
|  | 	return nRet; | |||
|  | } | |||
|  | 
 | |||
|  | void MusicUtils::stopMusic(unsigned int nSoundId) | |||
|  | { | |||
|  | 	MusicList::iterator p = getMciPlayerList().find(nSoundId); | |||
|  | 	if (p != getMciPlayerList().end()) | |||
|  | 	{ | |||
|  | 		p->second->stop(); | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | void MusicUtils::preloadMusic(TString pszFilePath) | |||
|  | { | |||
|  | 	if (pszFilePath.empty()) return; | |||
|  | 
 | |||
|  | 	int nRet = ::_Hash(pszFilePath); | |||
|  | 
 | |||
|  | 	if (getMciPlayerList().end() != getMciPlayerList().find(nRet)) return; | |||
|  | 
 | |||
|  | 	getMciPlayerList().insert(Music(nRet, new MciPlayer())); | |||
|  | 	MciPlayer * pPlayer = getMciPlayerList()[nRet]; | |||
|  | 	pPlayer->open(pszFilePath, nRet); | |||
|  | 
 | |||
|  | 	if (nRet == pPlayer->getSoundID()) return; | |||
|  | 
 | |||
|  | 	delete pPlayer; | |||
|  | 	getMciPlayerList().erase(nRet); | |||
|  | 	nRet = 0; | |||
|  | } | |||
|  | 
 | |||
|  | void MusicUtils::pauseMusic(unsigned int nSoundId) | |||
|  | { | |||
|  | 	MusicList::iterator p = getMciPlayerList().find(nSoundId); | |||
|  | 	if (p != getMciPlayerList().end()) | |||
|  | 	{ | |||
|  | 		p->second->pause(); | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | void MusicUtils::pauseAllMusics() | |||
|  | { | |||
|  | 	for (auto& iter : getMciPlayerList()) | |||
|  | 	{ | |||
|  | 		iter.second->pause(); | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | void MusicUtils::resumeMusic(unsigned int nSoundId) | |||
|  | { | |||
|  | 	MusicList::iterator p = getMciPlayerList().find(nSoundId); | |||
|  | 	if (p != getMciPlayerList().end()) | |||
|  | 	{ | |||
|  | 		p->second->resume(); | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | void MusicUtils::resumeAllMusics() | |||
|  | { | |||
|  | 	for (auto& iter : getMciPlayerList()) | |||
|  | 	{ | |||
|  | 		iter.second->resume(); | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | void MusicUtils::stopAllMusics() | |||
|  | { | |||
|  | 	for (auto& iter : getMciPlayerList()) | |||
|  | 	{ | |||
|  | 		iter.second->stop(); | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | void MusicUtils::unloadMusic(LPCTSTR pszFilePath) | |||
|  | { | |||
|  | 	unsigned int nID = ::_Hash(pszFilePath); | |||
|  | 	 | |||
|  | 	MusicList::iterator p = getMciPlayerList().find(nID); | |||
|  | 	if (p != getMciPlayerList().end()) | |||
|  | 	{ | |||
|  | 		SafeDelete(p->second); | |||
|  | 		getMciPlayerList().erase(nID); | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | unsigned int _Hash(TString key) | |||
|  | { | |||
|  | 	unsigned int len = unsigned(key.size()); | |||
|  | 	unsigned int hash = 0; | |||
|  | 
 | |||
|  | 	for (unsigned i = 0; i < len; i++) | |||
|  | 	{ | |||
|  | 		hash *= 16777619; | |||
|  | 		hash ^= (unsigned int)(unsigned char)toupper(key[i]); | |||
|  | 	} | |||
|  | 	return (hash); | |||
|  | } |