| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | #include "..\e2dtool.h"
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | e2d::Player::Player() | 
					
						
							|  |  |  |  | 	: _volume(1.f) | 
					
						
							| 
									
										
										
										
											2018-08-12 12:06:06 +08:00
										 |  |  |  | 	, _enabled(true) | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 	, _xAudio2(nullptr) | 
					
						
							|  |  |  |  | 	, _masteringVoice(nullptr) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-04 15:33:09 +08:00
										 |  |  |  | 	CoInitialize(nullptr); | 
					
						
							| 
									
										
										
										
											2018-08-12 12:06:06 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (FAILED(XAudio2Create(&_xAudio2, 0))) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		WARN("<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD> XAudio2 <20><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>"); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (FAILED(_xAudio2->CreateMasteringVoice(&_masteringVoice))) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		WARN("<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD> MasteringVoice <20><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>"); | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											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-21 22:57:21 +08:00
										 |  |  |  | 	if (!_musicList.empty()) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		for (auto pair : _musicList) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			delete pair.second; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (_masteringVoice) | 
					
						
							| 
									
										
										
										
											2018-08-12 12:06:06 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 		_masteringVoice->DestroyVoice(); | 
					
						
							| 
									
										
										
										
											2018-08-12 12:06:06 +08:00
										 |  |  |  | 		_masteringVoice = nullptr; | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	SafeRelease(_xAudio2); | 
					
						
							| 
									
										
										
										
											2018-07-04 15:33:09 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	CoUninitialize(); | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +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
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-12 12:06:06 +08:00
										 |  |  |  | IXAudio2MasteringVoice * e2d::Player::getMasteringVoice() | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-12 12:06:06 +08:00
										 |  |  |  | 	return _masteringVoice; | 
					
						
							| 
									
										
										
										
											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-08-12 12:06:06 +08:00
										 |  |  |  | 	Music * music = new (std::nothrow) Music(); | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-12 12:06:06 +08:00
										 |  |  |  | 	if (music && music->open(res)) | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		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; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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:48:04 +08:00
										 |  |  |  | 	if (_musicList.end() != _musicList.find(res)) | 
					
						
							|  |  |  |  | 		_musicList[res]->stop(); | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 20:06:27 +08:00
										 |  |  |  | float e2d::Player::getVolume() | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  |  | 	return _volume; | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 20:06:27 +08:00
										 |  |  |  | void e2d::Player::setVolume(float volume) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-28 20:06:27 +08:00
										 |  |  |  | 	_volume = std::min(std::max(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(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-12 12:06:06 +08:00
										 |  |  |  | void e2d::Player::setEnabled(bool enabled) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (_enabled == enabled) | 
					
						
							|  |  |  |  | 		return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	_enabled = enabled; | 
					
						
							|  |  |  |  | 	_enabled ? _xAudio2->StartEngine() : _xAudio2->StopEngine(); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  |  | 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-21 22:57:21 +08:00
										 |  |  |  | 		delete 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
										 |  |  |  | } |