fixed #228 Optimize function SimpleAudioEngine::stopBackgroundMusic

This commit is contained in:
natural-law 2010-11-30 14:37:11 +08:00
parent b1d3e66e68
commit 070439382e
4 changed files with 17 additions and 7 deletions

View File

@ -76,8 +76,9 @@ public:
/**
@brief Stop playing background music
@param bReleaseData If release the background music data or not.As default value is false
*/
void stopBackgroundMusic();
void stopBackgroundMusic(bool bReleaseData = false);
/**
@brief Pause playing background music

View File

@ -10,7 +10,7 @@ static SoundDataManager *s_pDataManager = NULL;
static SoundPlayer *s_pBackPlayer = NULL;
static TSoundPlayer *s_pEffectPlayer = NULL;
static int s_nBackMusicID = 0;
static int s_nBackgroundMusicVolume = 100;
static int s_nEffectsVolume = 100;
static bool s_bWillPlayBackgroundMusic = false;
@ -85,8 +85,8 @@ void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
nTimes = -1;
}
int nSoundID = s_pDataManager->loadSoundData(pszFilePath);
tEffectElement* pElement = s_pDataManager->getSoundData(nSoundID);
s_nBackMusicID = s_pDataManager->loadSoundData(pszFilePath);
tEffectElement* pElement = s_pDataManager->getSoundData(s_nBackMusicID);
if (pElement)
{
@ -94,12 +94,18 @@ void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
}
}
void SimpleAudioEngine::stopBackgroundMusic()
void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData)
{
if (s_pBackPlayer)
{
s_pBackPlayer->Stop();
}
if (bReleaseData && s_nBackMusicID)
{
s_pDataManager->unloadEffect(s_nBackMusicID);
s_nBackMusicID = 0;
}
}
void SimpleAudioEngine::pauseBackgroundMusic()

View File

@ -70,8 +70,10 @@ void SoundDataManager::setSoundResInfo(const T_SoundResInfo ResInfo[], int nCoun
int SoundDataManager::loadSoundData(const char* pszFilePath)
{
int nSoundID = 0;
SoundInfoMap::iterator iter;
iter = m_pSoundMap->find(pszFilePath);
if (! FileUtils::isFileExisted(pszFilePath))
if (iter != m_pSoundMap->end())
{
// if the file is not existed, find in the ResourceInfo
nSoundID = loadFromResourceInfo(pszFilePath);
@ -166,6 +168,7 @@ int SoundDataManager::loadFromFile(const char* pFilePath)
do
{
BREAK_IF(! FileUtils::isFileExisted(pFilePath));
int nID = BKDRHash(pFilePath);
// if we have loaded the file before,break

View File

@ -55,7 +55,7 @@ void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
s_Music.Play((bLoop) ? -1 : 1);
}
void SimpleAudioEngine::stopBackgroundMusic()
void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData)
{
s_Music.Stop();
}