Magic_Game/core/Tool/Player.cpp

211 lines
3.3 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 : _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)
{
if (filePath.isEmpty())
return false;
2018-05-17 23:53:27 +08:00
return preload(Resource(filePath));
2018-05-17 23:53:27 +08:00
}
bool e2d::Player::preload(const Resource& res)
2018-05-17 23:53:27 +08:00
{
if (_resList.end() != _resList.find(res))
2018-05-17 23:53:27 +08:00
return true;
Music * music = new (e2d::autorelease) Music();
if (music->open(res))
{
GC::retain(music);
music->setVolume(_volume);
_resList.insert(std::make_pair(res, music));
return true;
2018-05-17 23:53:27 +08:00
}
return false;
}
bool e2d::Player::play(const String& filePath, int nLoopCount)
{
if (filePath.isEmpty())
return false;
return play(Resource(filePath), nLoopCount);
2018-05-17 23:53:27 +08:00
}
bool e2d::Player::play(const Resource& res, int nLoopCount)
2018-05-17 23:53:27 +08:00
{
if (Player::preload(res))
2018-05-17 23:53:27 +08:00
{
auto music = _resList[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;
pause(Resource(filePath));
2018-05-17 23:53:27 +08:00
}
void e2d::Player::pause(const Resource& res)
2018-05-17 23:53:27 +08:00
{
if (_resList.end() != _resList.find(res))
_resList[res]->pause();
2018-05-17 23:53:27 +08:00
}
void e2d::Player::resume(const String& filePath)
{
if (filePath.isEmpty())
return;
resume(Resource(filePath));
2018-05-17 23:53:27 +08:00
}
void e2d::Player::resume(const Resource& res)
2018-05-17 23:53:27 +08:00
{
if (_resList.end() != _resList.find(res))
_resList[res]->pause();
2018-05-17 23:53:27 +08:00
}
void e2d::Player::stop(const String& filePath)
{
if (filePath.isEmpty())
return;
stop(Resource(filePath));
2018-05-17 23:53:27 +08:00
}
void e2d::Player::stop(const Resource& res)
2018-05-17 23:53:27 +08:00
{
if (res.isResource())
{
}
if (_resList.end() != _resList.find(res))
_resList[res]->stop();
2018-05-17 23:53:27 +08:00
}
bool e2d::Player::isPlaying(const String& filePath)
{
if (filePath.isEmpty())
return false;
return isPlaying(Resource(filePath));
2018-05-17 23:53:27 +08:00
}
bool e2d::Player::isPlaying(const Resource& res)
2018-05-17 23:53:27 +08:00
{
if (_resList.end() != _resList.find(res))
return _resList[res]->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 : _resList)
{
pair.second->setVolume(_volume);
}
2018-05-17 23:53:27 +08:00
}
void e2d::Player::pauseAll()
{
for (auto pair : _resList)
{
pair.second->pause();
}
2018-05-17 23:53:27 +08:00
}
void e2d::Player::resumeAll()
{
for (auto pair : _resList)
{
pair.second->resume();
}
2018-05-17 23:53:27 +08:00
}
void e2d::Player::stopAll()
{
for (auto pair : _resList)
{
pair.second->stop();
}
}
void e2d::Player::clearCache()
{
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
}