Cocos2d-x sound simpleaudioengine & game archive ccuserdefault

Source: Internet
Author: User


1. The sound of Cocos2d-x

The Cocos2d-iphone contains the cocosdenshion library, which provides three layers of interfaces from low to high: cdsoundengine, cdaudiomanager, and simpleaudioengine, but the entire library is fully dependent on openal. Since cocosdenshiono underlying support cannot be provided on other platforms, we only use the simpleaudioengine class at the top to implement cross-platform sound engines, which is very easy to use.

View the simpleaudioengine file to know the APIs for playing background music and sound effects. (The file location is as follows :)


Note: samples/CPP/testcpp/proj. there is an accompanying example under the IOS file directory, which contains a vast majority of examples, which are very detailed. It is a good demo collection for learning cocos2dx.


Example of simpleaudioengine sound engine in cocos2dx ---- cocosdenshiontest


(1) sound file pre-loading can improve the execution efficiency of the program in the game, but it cannot avoid increasing the memory usage.

// preload background music and effect    SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( MUSIC_FILE );    SimpleAudioEngine::sharedEngine()->preloadEffect( EFFECT_FILE );

The parameter is the name of the audio file. The two pre-loading methods show that the implementation of these methods will help us obtain the full path of the sound file.

void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath){    // Changing file path to full path    std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);    static_preloadBackgroundMusic(fullPath.c_str());}void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop){    // Changing file path to full path    std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);    static_playBackgroundMusic(fullPath.c_str(), bLoop);}


(2) end the use of simpleaudioengine in the onexit function.

void CocosDenshionTest::onExit(){    CCLayer::onExit();    SimpleAudioEngine::sharedEngine()->end();}


2. Game archives in Cocos2d-x

Supporting the game archive class ccuserdefault in Cocos2d-x can be used as a lightweight database.

(1) The file location is libs/cocos2dx/support/user_default.

Example of ccuserdefault in cocos2dx ---- ccuserdefaulttest

(2) This class file is relatively simple and used as a singleton class. You must use the shareduserdefault method to obtain the singleton object.

(3) The main APIs are set and get data methods.

For example:

/**    @brief Get bool value by key, if the key doesn't exist, a default value will return.     You can set the default value, or it is false.    */    bool    getBoolForKey(const char* pKey);    bool    getBoolForKey(const char* pKey, bool defaultValue);

/**    @brief Set bool value by key.    */    void    setBoolForKey(const char* pKey, bool value);

(4) Call the flush method to save the set data. View the implementation of this method

void CCUserDefault::flush(){    [[NSUserDefaults standardUserDefaults] synchronize];}

We know that the ccuserdefault is actually the nsuserdefault in OC.

(5) Note: using the ccuserdefault class to save data is to archive the data into an XML file-this is only for versions earlier than cocos2dx 2.1.2 (Why ?)

However, the current version I have installed is 2.1.4. the header file provides the following methods:

static void purgeSharedUserDefault();    const static std::string& getXMLFilePath();    static bool isXMLFileExist();

As soon as I saw it, it was obviously used to get the XML Path. I tried it:

CCUserDefault::sharedUserDefault()->setStringForKey("key", "value");    CCUserDefault::sharedUserDefault()->flush();        std::string myString = CCUserDefault::sharedUserDefault()->getStringForKey("key");    CCLog("value = %s",myString.c_str());        std::string str = CCUserDefault::getXMLFilePath();    CCLog("xml path_1 = %s",str.c_str());        bool isExit = CCUserDefault::isXMLFileExist();    if (isExit) {        CCLog("xml path_2 = %s",str.c_str());    }

The output result of the terminal is as follows:

Cocos2d: value = valueCocos2d: xml path_1 = /Users/ios/Library/Application Support/iPhone Simulator/6.1/Applications/A77F61F5-F763-499D-9505-E938091011EE/Library/Caches/UserDefault.xml


Apparently no output: XML path_2 indicates that the XML file does not exist !!

How is it possible? I checked the caches directory in the sandbox according to the path of XML path_1 !!

However, if we delete the first two lines of code and directly obtain the value based on the key, it is also found that there is data.

However, in the preferences directory, a plist file stores the data.

I looked for a while and finally found something !!

This class is implementing the initxmlfilepath method in the file.

void CCUserDefault::initXMLFilePath(){#ifdef KEEP_COMPATABILITY    if (! m_sbIsFilePathInitialized)    {        // xml file is stored in cache directory before 2.1.2        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);        NSString *documentsDirectory = [paths objectAtIndex:0];        m_sFilePath = [documentsDirectory UTF8String];        m_sFilePath.append("/");                m_sFilePath +=  XML_FILE_NAME;        m_sbIsFilePathInitialized = true;    }#endif}

// XML file is stored in cache directory before 2.1.2! It was saved to XML before version 2.1.2, but my current version is 2.1.4, So XML cannot be found, so the XML file cannot be found. Ah! In my opinion, the data in the current version of the ccuserdefault class is saved to the plist file under the preferences directory.


(6) another problem: this class provides the Set Method to add a data item, but does not provide the method to remove a data item. Therefore, you can only increase or decrease the number of data items.

About the static
Void purgeshareduserdefault (); obviously, this method clears this singleton object. View the implementation of this method

void CCUserDefault::purgeSharedUserDefault(){    m_spUserDefault = NULL;}

(7) Conclusion: the usage of this class ccuserdefault is very simple, and the provided data storage is limited (bool, Int, float, double, string ).

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.