| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | #include "..\e2dtool.h"
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | e2d::Player * e2d::Player::_instance = nullptr; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | e2d::Player::Player() | 
					
						
							|  |  |  |  | 	: _volume(1.f) | 
					
						
							|  |  |  |  | 	, _xAudio2(nullptr) | 
					
						
							|  |  |  |  | 	, _masteringVoice(nullptr) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | e2d::Player::~Player() | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 	for (auto pair : _fileList) | 
					
						
							|  |  |  |  | 		delete pair.second; | 
					
						
							|  |  |  |  | 	_fileList.clear(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	for (auto pair : _resList) | 
					
						
							|  |  |  |  | 		delete pair.second; | 
					
						
							|  |  |  |  | 	_resList.clear(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (_masteringVoice) | 
					
						
							|  |  |  |  | 		_masteringVoice->DestroyVoice(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	SafeRelease(_xAudio2); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | e2d::Player * e2d::Player::getInstance() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (!_instance) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		_instance = new (std::nothrow) Player; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		HRESULT hr; | 
					
						
							|  |  |  |  | 		if (FAILED(hr = XAudio2Create(&_instance->_xAudio2, 0)) || | 
					
						
							|  |  |  |  | 			FAILED(hr = _instance->_xAudio2->CreateMasteringVoice(&_instance->_masteringVoice))) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-07-03 23:39:00 +08:00
										 |  |  |  | 			throw SystemException(L"<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD> XAudio2 <20><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>"); | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	return _instance; | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | void e2d::Player::destroyInstance() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (_instance) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		delete _instance; | 
					
						
							|  |  |  |  | 		_instance = nullptr; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | IXAudio2 * e2d::Player::getIXAudio2() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	return _xAudio2; | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | bool e2d::Player::preload(const String& filePath) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	UINT hash = filePath.getHashCode(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 	if (_fileList.end() != _fileList.find(hash)) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		return true; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	else | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		Music * music = new (std::nothrow) Music(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (music->open(filePath)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 			music->setVolume(_volume); | 
					
						
							|  |  |  |  | 			_fileList.insert(std::pair<UINT, Music *>(hash, music)); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 			return true; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 		else | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			delete music; | 
					
						
							|  |  |  |  | 			music = nullptr; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	return false; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | bool e2d::Player::preload(int resNameId, const String& resType) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 	if (_resList.end() != _resList.find(resNameId)) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		return true; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	else | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		Music * music = new (std::nothrow) Music(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (music->open(resNameId, resType)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 			music->setVolume(_volume); | 
					
						
							|  |  |  |  | 			_resList.insert(std::pair<UINT, Music *>(resNameId, music)); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 			return true; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 		else | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			delete music; | 
					
						
							|  |  |  |  | 			music = nullptr; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	return false; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | bool e2d::Player::play(const String& filePath, int nLoopCount) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (Player::preload(filePath)) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		UINT hash = filePath.getHashCode(); | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 		auto music = _fileList[hash]; | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 		if (music->play(nLoopCount)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return true; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	return false; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | bool e2d::Player::play(int resNameId, const String& resType, int nLoopCount) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (Player::preload(resNameId, resType)) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 		auto music = _resList[resNameId]; | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 		if (music->play(nLoopCount)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return true; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	return false; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Player::pause(const String& filePath) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (filePath.isEmpty()) | 
					
						
							|  |  |  |  | 		return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	UINT hash = filePath.getHashCode(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 	if (_fileList.end() != _fileList.find(hash)) | 
					
						
							|  |  |  |  | 		_fileList[hash]->pause(); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Player::pause(int resNameId, const String& resType) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 	if (_resList.end() != _resList.find(resNameId)) | 
					
						
							|  |  |  |  | 		_resList[resNameId]->pause(); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Player::resume(const String& filePath) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (filePath.isEmpty()) | 
					
						
							|  |  |  |  | 		return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	UINT hash = filePath.getHashCode(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 	if (_fileList.end() != _fileList.find(hash)) | 
					
						
							|  |  |  |  | 		_fileList[hash]->resume(); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Player::resume(int resNameId, const String& resType) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 	if (_resList.end() != _resList.find(resNameId)) | 
					
						
							|  |  |  |  | 		_resList[resNameId]->pause(); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Player::stop(const String& filePath) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (filePath.isEmpty()) | 
					
						
							|  |  |  |  | 		return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	UINT hash = filePath.getHashCode(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 	if (_fileList.end() != _fileList.find(hash)) | 
					
						
							|  |  |  |  | 		_fileList[hash]->stop(); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Player::stop(int resNameId, const String& resType) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 	if (_resList.end() != _resList.find(resNameId)) | 
					
						
							|  |  |  |  | 		_resList[resNameId]->stop(); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | bool e2d::Player::isPlaying(const String& filePath) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (filePath.isEmpty()) | 
					
						
							|  |  |  |  | 		return false; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	UINT hash = filePath.getHashCode(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 	if (_fileList.end() != _fileList.find(hash)) | 
					
						
							|  |  |  |  | 		return _fileList[hash]->isPlaying(); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	return false; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | bool e2d::Player::isPlaying(int resNameId, const String& resType) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 	if (_resList.end() != _resList.find(resNameId)) | 
					
						
							|  |  |  |  | 		return _resList[resNameId]->isPlaying(); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 	return false; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | double e2d::Player::getVolume() | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 	return _volume; | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Player::setVolume(double volume) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-04 12:49:05 +08:00
										 |  |  |  | 	_volume = std::min(std::max(float(volume), -224.f), 224.f); | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 	for (auto pair : _fileList) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 		pair.second->setVolume(_volume); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Player::pauseAll() | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 	for (auto pair : _fileList) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		pair.second->pause(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Player::resumeAll() | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 	for (auto pair : _fileList) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		pair.second->resume(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Player::stopAll() | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 	for (auto pair : _fileList) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		pair.second->stop(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } |