Magic_Game/core/Tool/Player.cpp

276 lines
4.6 KiB
C++
Raw Normal View History

2018-05-17 23:53:27 +08:00
#include "..\e2dtool.h"
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
}
e2d::Player::~Player()
2018-05-17 23:53:27 +08:00
{
for (auto pair : _fileList)
2018-07-07 01:43:41 +08:00
GC::release(pair.second);
_fileList.clear();
for (auto pair : _resList)
2018-07-07 01:43:41 +08:00
GC::release(pair.second);
_resList.clear();
if (_masteringVoice)
_masteringVoice->DestroyVoice();
SafeRelease(_xAudio2);
2018-07-04 15:33:09 +08:00
CoUninitialize();
}
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>");
}
}
return _instance;
2018-05-17 23:53:27 +08:00
}
void e2d::Player::destroyInstance()
{
if (_instance)
{
delete _instance;
_instance = nullptr;
}
}
2018-05-17 23:53:27 +08:00
IXAudio2 * e2d::Player::getXAudio2()
{
return _xAudio2;
}
2018-05-17 23:53:27 +08:00
bool e2d::Player::preload(const String& filePath)
{
UINT hash = filePath.getHashCode();
if (_fileList.end() != _fileList.find(hash))
2018-05-17 23:53:27 +08:00
{
return true;
}
else
{
Music * music = new (e2d::autorelease) Music();
2018-05-17 23:53:27 +08:00
if (music->open(filePath))
{
2018-07-07 01:43:41 +08:00
GC::retain(music);
music->setVolume(_volume);
_fileList.insert(std::pair<UINT, Music *>(hash, music));
2018-05-17 23:53:27 +08:00
return true;
}
}
return false;
}
bool e2d::Player::preload(int resNameId, const String& resType)
{
if (_resList.end() != _resList.find(resNameId))
2018-05-17 23:53:27 +08:00
{
return true;
}
else
{
Music * music = new (e2d::autorelease) Music();
2018-05-17 23:53:27 +08:00
if (music->open(resNameId, resType))
{
2018-07-07 01:43:41 +08:00
GC::retain(music);
music->setVolume(_volume);
_resList.insert(std::pair<UINT, Music *>(resNameId, music));
2018-05-17 23:53:27 +08:00
return true;
}
}
return false;
}
bool e2d::Player::play(const String& filePath, int nLoopCount)
{
if (Player::preload(filePath))
{
UINT hash = filePath.getHashCode();
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))
{
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();
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)
{
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();
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)
{
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();
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)
{
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();
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)
{
if (_resList.end() != _resList.find(resNameId))
return _resList[resNameId]->isPlaying();
2018-05-17 23:53:27 +08:00
return false;
}
double e2d::Player::getVolume()
{
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);
for (auto pair : _fileList)
2018-05-17 23:53:27 +08:00
{
pair.second->setVolume(_volume);
2018-05-17 23:53:27 +08:00
}
for (auto pair : _resList)
{
pair.second->setVolume(_volume);
}
2018-05-17 23:53:27 +08:00
}
void e2d::Player::pauseAll()
{
for (auto pair : _fileList)
2018-05-17 23:53:27 +08:00
{
pair.second->pause();
}
for (auto pair : _resList)
{
pair.second->pause();
}
2018-05-17 23:53:27 +08:00
}
void e2d::Player::resumeAll()
{
for (auto pair : _fileList)
2018-05-17 23:53:27 +08:00
{
pair.second->resume();
}
for (auto pair : _resList)
{
pair.second->resume();
}
2018-05-17 23:53:27 +08:00
}
void e2d::Player::stopAll()
{
for (auto pair : _fileList)
2018-05-17 23:53:27 +08:00
{
pair.second->stop();
}
for (auto pair : _resList)
{
pair.second->stop();
}
}
void e2d::Player::clearCache()
{
for (auto pair : _fileList)
{
2018-07-07 01:43:41 +08:00
GC::release(pair.second);
}
_fileList.clear();
for (auto pair : _resList)
{
2018-07-07 01:43:41 +08:00
GC::release(pair.second);
}
_resList.clear();
2018-05-17 23:53:27 +08:00
}