| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | #include "..\e2dtool.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-02 14:30:48 +08:00
										 |  |  | e2d::Player * e2d::Player::_instance = nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | e2d::Player * e2d::Player::getInstance() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (!_instance) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		_instance = new (std::nothrow) Player; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return _instance; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::Player::destroyInstance() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (_instance) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		delete _instance; | 
					
						
							|  |  |  | 		_instance = nullptr; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  | e2d::Player::Player() | 
					
						
							|  |  |  | 	: _volume(1.f) | 
					
						
							| 
									
										
										
										
											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()) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-08-12 14:30:28 +08:00
										 |  |  | 		for (const auto& pair : _musicList) | 
					
						
							| 
									
										
										
										
											2018-07-21 22:57:21 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			delete pair.second; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool e2d::Player::preload(const String & filePath) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  | 	if (filePath.isEmpty()) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Music * music = new (std::nothrow) Music(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (music && music->open(filePath)) | 
					
						
							| 
									
										
										
										
											2018-08-12 12:06:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  | 		music->setVolume(_volume); | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 		_musicList.insert(std::make_pair(filePath.hash(), music)); | 
					
						
							|  |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2018-08-12 12:06:06 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | bool e2d::Player::play(const String & filePath, int nLoopCount) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  | 	if (filePath.isEmpty()) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2018-07-04 15:33:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 	if (Player::preload(filePath)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		auto music = _musicList[filePath.hash()]; | 
					
						
							|  |  |  | 		if (music->play(nLoopCount)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::Player::pause(const String & filePath) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  | 	if (filePath.isEmpty()) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	size_t hash = filePath.hash(); | 
					
						
							|  |  |  | 	if (_musicList.end() != _musicList.find(hash)) | 
					
						
							|  |  |  | 		_musicList[hash]->pause(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::Player::resume(const String & filePath) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  | 	if (filePath.isEmpty()) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	size_t hash = filePath.hash(); | 
					
						
							|  |  |  | 	if (_musicList.end() != _musicList.find(hash)) | 
					
						
							|  |  |  | 		_musicList[hash]->resume(); | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | void e2d::Player::stop(const String & filePath) | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  | 	if (filePath.isEmpty()) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	size_t hash = filePath.hash(); | 
					
						
							|  |  |  | 	if (_musicList.end() != _musicList.find(hash)) | 
					
						
							|  |  |  | 		_musicList[hash]->stop(); | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  | bool e2d::Player::isPlaying(const String & filePath) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  | 	if (filePath.isEmpty()) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	size_t hash = filePath.hash(); | 
					
						
							|  |  |  | 	if (_musicList.end() != _musicList.find(hash)) | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  | 		return _musicList[hash]->isPlaying(); | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											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-08-19 20:40:44 +08:00
										 |  |  | 	if (_musicList.end() != _musicList.find(res.resNameId)) | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  | 		music->setVolume(_volume); | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 		_musicList.insert(std::make_pair(res.resNameId, 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-08-19 20:40:44 +08:00
										 |  |  | 		auto music = _musicList[res.resNameId]; | 
					
						
							| 
									
										
										
										
											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-08-19 20:40:44 +08:00
										 |  |  | 	if (_musicList.end() != _musicList.find(res.resNameId)) | 
					
						
							|  |  |  | 		_musicList[res.resNameId]->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-08-19 20:40:44 +08:00
										 |  |  | 	if (_musicList.end() != _musicList.find(res.resNameId)) | 
					
						
							|  |  |  | 		_musicList[res.resNameId]->resume(); | 
					
						
							| 
									
										
										
										
											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-08-19 20:40:44 +08:00
										 |  |  | 	if (_musicList.end() != _musicList.find(res.resNameId)) | 
					
						
							|  |  |  | 		_musicList[res.resNameId]->stop(); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  | bool e2d::Player::isPlaying(const Resource& res) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 	if (_musicList.end() != _musicList.find(res.resNameId)) | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  | 		return _musicList[res.resNameId]->isPlaying(); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +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-08-23 16:37:51 +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-08-12 14:30:28 +08:00
										 |  |  | 	for (const auto& pair : _musicList) | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  | 		pair.second->setVolume(_volume); | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::Player::pauseAll() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-12 14:30:28 +08:00
										 |  |  | 	for (const 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-08-12 14:30:28 +08:00
										 |  |  | 	for (const 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-08-12 14:30:28 +08:00
										 |  |  | 	for (const auto& pair : _musicList) | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		pair.second->stop(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::Player::clearCache() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-12 14:30:28 +08:00
										 |  |  | 	for (const 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
										 |  |  | } |