Sound and sound effects of cocos2d-x (ii)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.