| 
									
										
										
										
											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-04 15:33:09 +08:00
										 |  |  |  | 	CoInitialize(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-08 02:48:04 +08:00
										 |  |  |  | 	for (auto pair : _musicList) | 
					
						
							| 
									
										
										
										
											2018-07-07 01:43:41 +08:00
										 |  |  |  | 		GC::release(pair.second); | 
					
						
							| 
									
										
										
										
											2018-07-08 02:48:04 +08:00
										 |  |  |  | 	_musicList.clear(); | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (_masteringVoice) | 
					
						
							|  |  |  |  | 		_masteringVoice->DestroyVoice(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	SafeRelease(_xAudio2); | 
					
						
							| 
									
										
										
										
											2018-07-04 15:33:09 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	CoUninitialize(); | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 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-05 01:35:50 +08:00
										 |  |  |  | IXAudio2 * e2d::Player::getXAudio2() | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	return _xAudio2; | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | bool e2d::Player::preload(const String& filePath) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	if (filePath.isEmpty()) | 
					
						
							|  |  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	return preload(Resource(filePath)); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | bool e2d::Player::preload(const Resource& res) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:48:04 +08:00
										 |  |  |  | 	if (_musicList.end() != _musicList.find(res)) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 		return true; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	Music * music = new (e2d::autorelease) Music(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (music->open(res)) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		GC::retain(music); | 
					
						
							|  |  |  |  | 		music->setVolume(_volume); | 
					
						
							| 
									
										
										
										
											2018-07-08 02:48:04 +08:00
										 |  |  |  | 		_musicList.insert(std::make_pair(res, music)); | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 	return false; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | bool e2d::Player::play(const String& filePath, int nLoopCount) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	if (filePath.isEmpty()) | 
					
						
							|  |  |  |  | 		return false; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	return play(Resource(filePath), nLoopCount); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | bool e2d::Player::play(const Resource& res, int nLoopCount) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	if (Player::preload(res)) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-07-08 02:48:04 +08:00
										 |  |  |  | 		auto music = _musicList[res]; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	pause(Resource(filePath)); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | void e2d::Player::pause(const Resource& res) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:48:04 +08:00
										 |  |  |  | 	if (_musicList.end() != _musicList.find(res)) | 
					
						
							|  |  |  |  | 		_musicList[res]->pause(); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Player::resume(const String& filePath) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (filePath.isEmpty()) | 
					
						
							|  |  |  |  | 		return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	resume(Resource(filePath)); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | void e2d::Player::resume(const Resource& res) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:48:04 +08:00
										 |  |  |  | 	if (_musicList.end() != _musicList.find(res)) | 
					
						
							|  |  |  |  | 		_musicList[res]->pause(); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Player::stop(const String& filePath) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (filePath.isEmpty()) | 
					
						
							|  |  |  |  | 		return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	stop(Resource(filePath)); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | void e2d::Player::stop(const Resource& res) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	if (res.isResource()) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-08 02:48:04 +08:00
										 |  |  |  | 	if (_musicList.end() != _musicList.find(res)) | 
					
						
							|  |  |  |  | 		_musicList[res]->stop(); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | bool e2d::Player::isPlaying(const String& filePath) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (filePath.isEmpty()) | 
					
						
							|  |  |  |  | 		return false; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	return isPlaying(Resource(filePath)); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | bool e2d::Player::isPlaying(const Resource& res) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:48:04 +08:00
										 |  |  |  | 	if (_musicList.end() != _musicList.find(res)) | 
					
						
							|  |  |  |  | 		return _musicList[res]->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-08 02:48:04 +08:00
										 |  |  |  | 	for (auto pair : _musicList) | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		pair.second->setVolume(_volume); | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Player::pauseAll() | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:48:04 +08:00
										 |  |  |  | 	for (auto pair : _musicList) | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		pair.second->pause(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Player::resumeAll() | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:48:04 +08:00
										 |  |  |  | 	for (auto pair : _musicList) | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		pair.second->resume(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Player::stopAll() | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:48:04 +08:00
										 |  |  |  | 	for (auto pair : _musicList) | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		pair.second->stop(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Player::clearCache() | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:48:04 +08:00
										 |  |  |  | 	for (auto pair : _musicList) | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-07-07 01:43:41 +08:00
										 |  |  |  | 		GC::release(pair.second); | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-08 02:48:04 +08:00
										 |  |  |  | 	_musicList.clear(); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } |