Magic_Game/Easy2D/Tool/EMusicUtils.cpp

510 lines
11 KiB
C++
Raw Normal View History

#include "..\etools.h"
#include "..\Win\winbase.h"
#include <mmsystem.h>
#pragma comment(lib , "winmm.lib")
#include <map>
#include <Digitalv.h>
////////////////////////////////////////////////////////////////////
// MciPlayer
////////////////////////////////////////////////////////////////////
class MciPlayer
{
public:
MciPlayer();
~MciPlayer();
void close();
void open(const e2d::EString & pFileName, UINT uId);
void open(const e2d::EString & pResouceName, const e2d::EString & pResouceType, const e2d::EString & musicExtension, UINT uId);
void play(bool bLoop = false);
void pause();
void resume();
void stop();
void rewind();
void setVolume(float volume);
bool isPlaying();
UINT getSoundID();
private:
void _sendCommand(int nCommand, DWORD_PTR param1 = 0, DWORD_PTR parma2 = 0);
MCIDEVICEID m_dev;
UINT m_nSoundID;
bool m_bPlaying;
bool m_bLoop;
e2d::EString m_sTmpFileName;
};
MciPlayer::MciPlayer() :
m_dev(0L),
m_nSoundID(0),
m_bPlaying(false),
m_bLoop(false)
{
}
MciPlayer::~MciPlayer()
{
close(); // <20>رղ<D8B1><D5B2><EFBFBD><EFBFBD><EFBFBD>
}
void MciPlayer::open(const e2d::EString & pFileName, UINT uId)
{
// <20><><EFBFBD>Բ<EFBFBD><D4B2><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5>ļ<EFBFBD>
if (pFileName.empty()) return;
// ֹͣ<CDA3><D6B9>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
close();
// <20><><EFBFBD><EFBFBD> MCI_OPEN_PARMS <20><><EFBFBD><EFBFBD>
MCI_OPEN_PARMS mciOpen = { 0 };
mciOpen.lpstrDeviceType = (LPCTSTR)-1;
mciOpen.lpstrElementName = pFileName.c_str();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
MCIERROR mciError;
mciError = mciSendCommand(0, MCI_OPEN, MCI_OPEN_ELEMENT, reinterpret_cast<DWORD_PTR>(&mciOpen));
// <20><><EFBFBD>ִ<EFBFBD><D6B4><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD>
if (mciError) return;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD>Ϣ
m_dev = mciOpen.wDeviceID;
m_nSoundID = uId;
m_bPlaying = false;
}
void MciPlayer::open(const e2d::EString & pResouceName, const e2d::EString & pResouceType, const e2d::EString & musicExtension, UINT uId)
{
bool ExtractResource(LPCTSTR strDstFile, LPCTSTR strResType, LPCTSTR strResName);
// <20><><EFBFBD>Բ<EFBFBD><D4B2><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5>ļ<EFBFBD>
if (pResouceName.empty() || pResouceType.empty()) return;
// <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD>ļ<EFBFBD>Ŀ¼
TCHAR tmpFilePath[_MAX_PATH];
::GetTempPath(_MAX_PATH, tmpFilePath);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ļ<EFBFBD>Ŀ¼
e2d::EString tmpFileName = tmpFilePath + e2d::EApp::getAppName();
if (_waccess(tmpFileName.c_str(), 0) == -1)
{
_wmkdir(tmpFileName.c_str());
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
tmpFileName.append(L"\\");
tmpFileName.append(std::to_wstring(uId));
tmpFileName.append(L"." + musicExtension);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԴΪ<D4B4><CEAA>ʱ<EFBFBD>ļ<EFBFBD>
if (ExtractResource(tmpFileName.c_str(), pResouceType.c_str(), pResouceName.c_str()))
{
// ֹͣ<CDA3><D6B9>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
close();
// <20><><EFBFBD><EFBFBD> MCI_OPEN_PARMS <20><><EFBFBD><EFBFBD>
MCI_OPEN_PARMS mciOpen = { 0 };
mciOpen.lpstrDeviceType = (LPCTSTR)-1;
mciOpen.lpstrElementName = tmpFileName.c_str();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
MCIERROR mciError;
mciError = mciSendCommand(0, MCI_OPEN, MCI_OPEN_ELEMENT, reinterpret_cast<DWORD_PTR>(&mciOpen));
// <20><><EFBFBD>ִ<EFBFBD><D6B4><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD>
if (mciError) return;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD>Ϣ
m_dev = mciOpen.wDeviceID;
m_nSoundID = uId;
m_bPlaying = false;
m_sTmpFileName = tmpFileName;
}
}
void MciPlayer::play(bool bLoop)
{
// <20>豸Ϊ<E8B1B8><CEAA>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD>
if (!m_dev)
{
return;
}
// <20><><EFBFBD>ò<EFBFBD><C3B2>Ų<EFBFBD><C5B2><EFBFBD>
MCI_PLAY_PARMS mciPlay = { 0 };
MCIERROR s_mciError;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
s_mciError = mciSendCommand(m_dev, MCI_PLAY, MCI_FROM | (bLoop ? MCI_DGV_PLAY_REPEAT : 0), reinterpret_cast<DWORD_PTR>(&mciPlay));
// δ<><CEB4><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD> m_bPlaying Ϊ true
if (!s_mciError)
{
m_bPlaying = true;
m_bLoop = bLoop;
}
}
void MciPlayer::close()
{
// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
if (m_bPlaying)
{
stop();
}
// <20>ر<EFBFBD><D8B1>
if (m_dev)
{
_sendCommand(MCI_CLOSE);
}
// ɾ<><C9BE><EFBFBD><EFBFBD>ʱ<EFBFBD>ļ<EFBFBD>
if (!m_sTmpFileName.empty())
{
DeleteFile(m_sTmpFileName.c_str());
m_sTmpFileName.clear();
}
// <20>ָ<EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
m_dev = 0;
m_bPlaying = false;
}
void MciPlayer::pause()
{
// <20><>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD>
_sendCommand(MCI_PAUSE);
m_bPlaying = false;
}
void MciPlayer::resume()
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
_sendCommand(MCI_RESUME);
m_bPlaying = true;
}
void MciPlayer::stop()
{
// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
_sendCommand(MCI_STOP);
m_bPlaying = false;
}
void MciPlayer::rewind()
{
// <20>豸Ϊ<E8B1B8><CEAA>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD>
if (!m_dev)
{
return;
}
// <20><><EFBFBD>ò<EFBFBD><C3B2><EFBFBD>λ<EFBFBD><CEBB>
mciSendCommand(m_dev, MCI_SEEK, MCI_SEEK_TO_START, 0);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
MCI_PLAY_PARMS mciPlay = { 0 };
m_bPlaying = mciSendCommand(m_dev, MCI_PLAY, (m_bLoop ? MCI_DGV_PLAY_REPEAT : 0), reinterpret_cast<DWORD_PTR>(&mciPlay)) ? false : true;
}
void MciPlayer::setVolume(float volume)
{
MCI_DGV_SETAUDIO_PARMS mciSetAudioPara = { 0 };
mciSetAudioPara.dwItem = MCI_DGV_SETAUDIO_VOLUME;
mciSetAudioPara.dwValue = DWORD(1000 * min(max(volume, 0), 1));
mciSendCommand(m_dev, MCI_SETAUDIO, MCI_DGV_SETAUDIO_VALUE | MCI_DGV_SETAUDIO_ITEM, (DWORD_PTR)&mciSetAudioPara);
}
bool MciPlayer::isPlaying()
{
return m_bPlaying;
}
UINT MciPlayer::getSoundID()
{
return m_nSoundID;
}
void MciPlayer::_sendCommand(int nCommand, DWORD_PTR param1, DWORD_PTR parma2)
{
// <20><><EFBFBD>豸ʱ<E8B1B8><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD>
if (!m_dev)
{
return;
}
// <20><><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><E8B1B8><EFBFBD>Ͳ<EFBFBD><CDB2><EFBFBD>
mciSendCommand(m_dev, nCommand, param1, parma2);
}
////////////////////////////////////////////////////////////////////
// e2d::EMusicUtils
////////////////////////////////////////////////////////////////////
typedef std::map<unsigned int, MciPlayer *> MusicList;
typedef std::pair<unsigned int, MciPlayer *> Music;
static size_t Hash(const e2d::EString & key);
static MusicList& getMciPlayerList()
{
static MusicList s_List;
return s_List;
}
static MciPlayer& getBgMciPlayer()
{
static MciPlayer s_Music;
return s_Music;
}
void e2d::EMusicUtils::end()
{
// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
getBgMciPlayer().close();
// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (auto& iter : getMciPlayerList())
{
SafeDelete(&iter.second);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>
getMciPlayerList().clear();
return;
}
void e2d::EMusicUtils::setVolume(float volume)
{
// <20><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
getBgMciPlayer().setVolume(volume);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (auto& iter : getMciPlayerList())
{
iter.second->setVolume(volume);
}
}
void e2d::EMusicUtils::setVolume(const EString & musicFilePath, float volume)
{
unsigned int nRet = ::Hash(musicFilePath);
MusicList::iterator p = getMciPlayerList().find(nRet);
if (p != getMciPlayerList().end())
{
p->second->setVolume(volume);
}
}
void e2d::EMusicUtils::playBackgroundMusic(const EString & musicFilePath, bool bLoop)
{
if (musicFilePath.empty())
{
return;
}
getBgMciPlayer().open(musicFilePath, ::Hash(musicFilePath));
getBgMciPlayer().play(bLoop);
}
void e2d::EMusicUtils::playBackgroundMusic(const EString & musicResourceName, const EString & musicResourceType, const EString & musicExtension, bool loop)
{
if (musicResourceName.empty() || musicResourceType.empty())
{
return;
}
getBgMciPlayer().open(musicResourceName, musicResourceType, musicExtension, ::Hash(musicResourceName));
getBgMciPlayer().play(loop);
}
void e2d::EMusicUtils::stopBackgroundMusic(bool bReleaseData)
{
if (bReleaseData)
{
getBgMciPlayer().close();
}
else
{
getBgMciPlayer().stop();
}
}
void e2d::EMusicUtils::pauseBackgroundMusic()
{
getBgMciPlayer().pause();
}
void e2d::EMusicUtils::resumeBackgroundMusic()
{
getBgMciPlayer().resume();
}
void e2d::EMusicUtils::rewindBackgroundMusic()
{
getBgMciPlayer().rewind();
}
bool e2d::EMusicUtils::isBackgroundMusicPlaying()
{
return getBgMciPlayer().isPlaying();
}
void e2d::EMusicUtils::setBackgroundMusicVolume(float volume)
{
getBgMciPlayer().setVolume(volume);
}
void e2d::EMusicUtils::playMusic(const EString & musicFilePath, bool bLoop)
{
unsigned int nRet = ::Hash(musicFilePath);
preloadMusic(musicFilePath);
MusicList::iterator p = getMciPlayerList().find(nRet);
if (p != getMciPlayerList().end())
{
p->second->play(bLoop);
}
}
void e2d::EMusicUtils::playMusic(const EString & musicResourceName, const EString & musicResourceType, const EString & musicExtension, bool loop)
{
unsigned int nRet = ::Hash(musicResourceName);
preloadMusic(musicResourceName, musicResourceType, musicExtension);
MusicList::iterator p = getMciPlayerList().find(nRet);
if (p != getMciPlayerList().end())
{
p->second->play(loop);
}
}
void e2d::EMusicUtils::_stopMusic(size_t nSoundId)
{
MusicList::iterator p = getMciPlayerList().find(nSoundId);
if (p != getMciPlayerList().end())
{
p->second->stop();
}
}
void e2d::EMusicUtils::preloadMusic(const EString & musicFilePath)
{
if (musicFilePath.empty()) return;
int nRet = ::Hash(musicFilePath);
if (getMciPlayerList().end() != getMciPlayerList().find(nRet)) return;
getMciPlayerList().insert(Music(nRet, new MciPlayer()));
MciPlayer * pPlayer = getMciPlayerList()[nRet];
pPlayer->open(musicFilePath, nRet);
if (nRet == pPlayer->getSoundID()) return;
delete pPlayer;
getMciPlayerList().erase(nRet);
nRet = 0;
}
void e2d::EMusicUtils::preloadMusic(const EString & musicResourceName, const EString & musicResourceType, const EString & musicExtension)
{
if (musicResourceName.empty() || musicResourceType.empty()) return;
int nRet = ::Hash(musicResourceName);
if (getMciPlayerList().end() != getMciPlayerList().find(nRet)) return;
getMciPlayerList().insert(Music(nRet, new MciPlayer()));
MciPlayer * pPlayer = getMciPlayerList()[nRet];
pPlayer->open(musicResourceName, musicResourceType, musicExtension, nRet);
if (nRet == pPlayer->getSoundID()) return;
delete pPlayer;
getMciPlayerList().erase(nRet);
nRet = 0;
}
void e2d::EMusicUtils::_pauseMusic(size_t nSoundId)
{
MusicList::iterator p = getMciPlayerList().find(nSoundId);
if (p != getMciPlayerList().end())
{
p->second->pause();
}
}
void e2d::EMusicUtils::pauseAllMusics()
{
for (auto& iter : getMciPlayerList())
{
iter.second->pause();
}
}
void e2d::EMusicUtils::_resumeMusic(size_t nSoundId)
{
MusicList::iterator p = getMciPlayerList().find(nSoundId);
if (p != getMciPlayerList().end())
{
p->second->resume();
}
}
void e2d::EMusicUtils::resumeAllMusics()
{
for (auto& iter : getMciPlayerList())
{
iter.second->resume();
}
}
void e2d::EMusicUtils::stopAllMusics()
{
for (auto& iter : getMciPlayerList())
{
iter.second->stop();
}
}
void e2d::EMusicUtils::unloadMusic(const EString & musicFilePath)
{
unsigned int nID = ::Hash(musicFilePath);
MusicList::iterator p = getMciPlayerList().find(nID);
if (p != getMciPlayerList().end())
{
SafeDelete(&p->second);
getMciPlayerList().erase(nID);
}
}
size_t Hash(const e2d::EString & key)
{
static std::hash<e2d::EString> h;
return h(key);
}
bool ExtractResource(LPCTSTR strDstFile, LPCTSTR strResType, LPCTSTR strResName)
{
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
HANDLE hFile = ::CreateFile(strDstFile, GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return false;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>ļ<EFBFBD><C4BC>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD>ڴ桢<DAB4>õ<EFBFBD><C3B5><EFBFBD>Դ<EFBFBD><D4B4>С
HRSRC hRes = ::FindResource(NULL, strResName, strResType);
HGLOBAL hMem = ::LoadResource(NULL, hRes);
DWORD dwSize = ::SizeofResource(NULL, hRes);
// д<><D0B4><EFBFBD>ļ<EFBFBD>
DWORD dwWrite = 0; // <20><><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>ֽ<EFBFBD>
::WriteFile(hFile, hMem, dwSize, &dwWrite, NULL);
::CloseHandle(hFile);
return true;
}