Magic_Game/Easy2D/Tool/EMusicUtils.cpp

157 lines
3.2 KiB
C++
Raw Normal View History

#include "..\etools.h"
#include <mmsystem.h>
#include "..\Win\MciPlayer.h"
#include <map>
typedef std::pair<UINT, MciPlayer *> Music;
typedef std::map<UINT, MciPlayer *> MusicList;
static UINT Hash(const e2d::EString & key)
{
static std::hash<e2d::EString> h;
return h(key);
}
static MusicList& getMciPlayerList()
{
static MusicList s_List;
return s_List;
}
UINT e2d::EMusicUtils::playMusic(const EString & musicFilePath, int repeatTimes)
{
UINT nRet = preloadMusic(musicFilePath);
if (nRet)
{
getMciPlayerList()[nRet]->play(repeatTimes);
}
return nRet;
}
UINT e2d::EMusicUtils::playMusic(const EString & musicResourceName, const EString & musicResourceType, const EString & musicExtension, int repeatTimes)
{
UINT nRet = preloadMusic(musicResourceName, musicResourceType, musicExtension);
2017-10-31 17:19:13 +08:00
if (nRet)
{
getMciPlayerList()[nRet]->play(repeatTimes);
}
return nRet;
}
UINT e2d::EMusicUtils::preloadMusic(const EString & musicFilePath)
{
if (musicFilePath.empty())
return 0;
UINT nRet = ::Hash(musicFilePath);
if (getMciPlayerList().end() != getMciPlayerList().find(nRet))
return nRet;
2017-10-31 17:19:13 +08:00
getMciPlayerList().insert(Music(nRet, new MciPlayer()));
MciPlayer * pPlayer = getMciPlayerList()[nRet];
pPlayer->open(musicFilePath, nRet);
if (nRet == pPlayer->getMusicID()) return nRet;
delete pPlayer;
getMciPlayerList().erase(nRet);
return 0;
}
UINT e2d::EMusicUtils::preloadMusic(const EString & musicResourceName, const EString & musicResourceType, const EString & musicExtension)
{
if (musicResourceName.empty() || musicResourceType.empty())
return 0;
UINT nRet = ::Hash(musicResourceName);
if (getMciPlayerList().end() != getMciPlayerList().find(nRet))
return nRet;
getMciPlayerList().insert(Music(nRet, new MciPlayer()));
MciPlayer * pPlayer = getMciPlayerList()[nRet];
pPlayer->open(musicResourceName, musicResourceType, musicExtension, nRet);
if (nRet == pPlayer->getMusicID()) return nRet;
delete pPlayer;
getMciPlayerList().erase(nRet);
return 0;
}
bool e2d::EMusicUtils::resumeMusic(UINT musicId)
{
MusicList::iterator p = getMciPlayerList().find(musicId);
if (p != getMciPlayerList().end())
{
p->second->resume();
return true;
}
return false;
}
bool e2d::EMusicUtils::resumeMusic(const EString & musicName)
{
return resumeMusic(Hash(musicName));;
}
bool e2d::EMusicUtils::pauseMusic(UINT musicId)
{
MusicList::iterator p = getMciPlayerList().find(musicId);
if (p != getMciPlayerList().end())
{
p->second->pause();
return true;
}
return false;
}
bool e2d::EMusicUtils::pauseMusic(const EString & musicName)
{
return pauseMusic(Hash(musicName));
}
bool e2d::EMusicUtils::stopMusic(UINT musicId)
{
MusicList::iterator p = getMciPlayerList().find(musicId);
if (p != getMciPlayerList().end())
{
p->second->stop();
return true;
}
return false;
}
bool e2d::EMusicUtils::stopMusic(const EString & musicName)
{
return stopMusic(Hash(musicName));;
}
void e2d::EMusicUtils::pauseAllMusics()
{
for (auto& iter : getMciPlayerList())
{
iter.second->pause();
}
}
void e2d::EMusicUtils::resumeAllMusics()
{
for (auto& iter : getMciPlayerList())
{
iter.second->resume();
}
}
void e2d::EMusicUtils::stopAllMusics()
{
for (auto& iter : getMciPlayerList())
{
iter.second->stop();
}
}