In the previous article, wrote the Cocos2d-x 2.1.6 version of the sound and sound effects of playback, the result is written as follows code:
float M_musicvol = 0.5f; (0.0f~1.0f) Volume size
float M_wavvol = 0.5f; (0.0f~1.0f) Sound size
Cocosdenshion::simpleaudioengine::sharedengine ()->setbackgroundmusicvolume (M_MusicVol);
Cocosdenshion::simpleaudioengine::sharedengine ()->seteffectsvolume (M_wavvol);
The settings are not valid, viewed, and later found that in the code there is no related processing, so reference cocos2d-x in the testcpp to modify it, presumably as follows:
(1) Open the MciPlayer.h file, add the header file: #include <digitalv.h>, and add the interface as follows:
Class Mciplayer
{
Public
/*
Set the volume size 0~1000
*/
void SetVolume (UINT volume);
/*
Get volume size 0~1000
*/
UINT getvolume () const;
};
(2) Open the MciPlayer.cpp file, the above interface implementation code is as follows:
void Mciplayer::setvolume (UINT volume)
{
if (!m_hdev)
Return
Mci_dgv_setaudio_parms Mciparams = {0};
Mciparams.dwitem = Mci_dgv_setaudio_volume;
Mciparams.dwvalue = volume;
Mcisendcommand (M_hdev, Mci_setaudio,
Mci_dgv_setaudio_item | Mci_dgv_setaudio_value, (DWORD) &mciparams);
}
UINT Mciplayer::getvolume () const
{
if (!m_hdev)
return 0;
Mci_status_parms Mciparams = {0};
Mciparams.dwitem = Mci_dgv_status_volume;
Mcisendcommand (M_hdev, MCi_STATUS, Mci_status_item, (DWORD) &mciparams);
return mciparams.dwreturn;
}
(3) Open SimpleAudioEngine.h, add the following code:
Class Export_dll Simpleaudioengine:public TypeInfo
{
Private
float M_effectsvolume; Sound volume
};
(4) Open SimpleAudioEngine.cpp, add the following code:
Simpleaudioengine::simpleaudioengine ():
M_effectsvolume (1.0f)//Sound volume initialization
{
}
unsigned int simpleaudioengine::p layeffect (const char* Pszfilepath, BOOL bloop)
{
unsigned int nret = _hash (Pszfilepath);
Preloadeffect (Pszfilepath);
Effectlist::iterator p = sharedlist (). Find (Nret);
if (P! = sharedlist (). End ())
{
P->second->play ((bloop) -1:1);
P->second->setvolume (UINT) (M_effectsvolume * 1000.0f)); New Code Additions
}
return nret;
}
Volume interface
Float Simpleaudioengine::getbackgroundmusicvolume ()
{
//return 1.0;
Return Sharedmusic (). Getvolume ()/1000.0f;
}
void Simpleaudioengine::setbackgroundmusicvolume (float volume)
{
Sharedmusic (). SetVolume (UINT) (volume * 1000.0f));
}
Float Simpleaudioengine::geteffectsvolume ()
{
//return 1.0;
return m_effectsvolume;
}
void Simpleaudioengine::seteffectsvolume (float volume)
{
M_effectsvolume = volume;
Effectlist::iterator ITER;
for (iter = Sharedlist (). Begin (); ITER! = Sharedlist (). end (); iter++)
{
Iter->second->setvolume (UINT) (volume * 1000.0f));
}
}
Next compile the code, after success, you can control the volume size OH.
Sound and sound effects of cocos2d-x (ii)