| 
									
										
										
										
											2017-10-20 00:59:26 +08:00
										 |  |  | #include "..\etools.h"
 | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | #include <mmsystem.h>
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | #include "..\Win\MciPlayer.h"
 | 
					
						
							|  |  |  | #include <map>
 | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | typedef std::pair<UINT, MciPlayer *> Music; | 
					
						
							|  |  |  | typedef std::map<UINT, MciPlayer *> MusicList; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | static UINT Hash(const e2d::EString & key) | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	static std::hash<e2d::EString> h; | 
					
						
							|  |  |  | 	return h(key); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | static MusicList& getMciPlayerList() | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	static MusicList s_List; | 
					
						
							|  |  |  | 	return s_List; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | UINT e2d::EMusicUtils::playMusic(const EString & musicFilePath, int repeatTimes) | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	UINT nRet = preloadMusic(musicFilePath); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	if (nRet) | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 		getMciPlayerList()[nRet]->play(repeatTimes); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	return nRet; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | UINT e2d::EMusicUtils::playMusic(const EString & musicResourceName, const EString & musicResourceType, const EString & musicExtension, int repeatTimes) | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	UINT nRet = preloadMusic(musicResourceName, musicResourceType, musicExtension); | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	if (nRet) | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 		getMciPlayerList()[nRet]->play(repeatTimes); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	return nRet; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | UINT e2d::EMusicUtils::preloadMusic(const EString & musicFilePath) | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	if (musicFilePath.empty())  | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	UINT nRet = ::Hash(musicFilePath); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	if (getMciPlayerList().end() != getMciPlayerList().find(nRet))  | 
					
						
							|  |  |  | 		return nRet; | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	getMciPlayerList().insert(Music(nRet, new MciPlayer())); | 
					
						
							|  |  |  | 	MciPlayer * pPlayer = getMciPlayerList()[nRet]; | 
					
						
							|  |  |  | 	pPlayer->open(musicFilePath, nRet); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	if (nRet == pPlayer->getMusicID()) return nRet; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	delete pPlayer; | 
					
						
							|  |  |  | 	getMciPlayerList().erase(nRet); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | UINT e2d::EMusicUtils::preloadMusic(const EString & musicResourceName, const EString & musicResourceType, const EString & musicExtension) | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	if (musicResourceName.empty() || musicResourceType.empty())  | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	UINT nRet = ::Hash(musicResourceName); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	if (getMciPlayerList().end() != getMciPlayerList().find(nRet))  | 
					
						
							|  |  |  | 		return nRet; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	getMciPlayerList().insert(Music(nRet, new MciPlayer())); | 
					
						
							|  |  |  | 	MciPlayer * pPlayer = getMciPlayerList()[nRet]; | 
					
						
							|  |  |  | 	pPlayer->open(musicResourceName, musicResourceType, musicExtension, nRet); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	if (nRet == pPlayer->getMusicID()) return nRet; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	delete pPlayer; | 
					
						
							|  |  |  | 	getMciPlayerList().erase(nRet); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | bool e2d::EMusicUtils::resumeMusic(UINT musicId) | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	MusicList::iterator p = getMciPlayerList().find(musicId); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 	if (p != getMciPlayerList().end()) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 		p->second->resume(); | 
					
						
							|  |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | bool e2d::EMusicUtils::resumeMusic(const EString & musicName) | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	return resumeMusic(Hash(musicName));; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | bool e2d::EMusicUtils::pauseMusic(UINT musicId) | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	MusicList::iterator p = getMciPlayerList().find(musicId); | 
					
						
							|  |  |  | 	if (p != getMciPlayerList().end()) | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 		p->second->pause(); | 
					
						
							|  |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | bool e2d::EMusicUtils::pauseMusic(const EString & musicName) | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	return pauseMusic(Hash(musicName)); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | bool e2d::EMusicUtils::stopMusic(UINT musicId) | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	MusicList::iterator p = getMciPlayerList().find(musicId); | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 	if (p != getMciPlayerList().end()) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		p->second->stop(); | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | bool e2d::EMusicUtils::stopMusic(const EString & musicName) | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-08 15:37:52 +08:00
										 |  |  | 	return stopMusic(Hash(musicName));; | 
					
						
							| 
									
										
										
										
											2017-10-25 10:54:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::EMusicUtils::pauseAllMusics() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	for (auto& iter : getMciPlayerList()) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		iter.second->pause(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::EMusicUtils::resumeAllMusics() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	for (auto& iter : getMciPlayerList()) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		iter.second->resume(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::EMusicUtils::stopAllMusics() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	for (auto& iter : getMciPlayerList()) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		iter.second->stop(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |