22. Cocos2dx 3.0 game development: If there is no music in the world, will your ears be alone?

Source: Internet
Author: User

 

If there is no music in the world, in the forest, we will not be able to hear the ying and the birds sing with pure and pure voices; nor will we be able to hear the sound of the beetle and the grasshopper's violin; I can't hear the stream playing, nor can I hear the fallen leaves singing ...... No matter how lush or beautiful the forest is, it is just empty and lonely.

If there is no music in the world, people will not be able to hear those beautiful music created by music talents such as Mozart and Beethoven; people will not be able to see the vigorous performances of the flower band; the musical Sands will also leave us. The national flag will not be accompanied by the impassioned march of the volunteers; the music lessons in our curriculum will disappear ...... Our lives will become monotonous and boring. Without music, the world will become unimaginable, and the beauty and highlights of life will be eclipsed. ...... I ran O (question _ Answer) O ha!
Visual, tactile, and auditory are three forms of interaction between players and games. Each form is very important. Implementing a set of high-quality music and sound effects in a game is much easier than making beautiful pictures. Developers can increase the Interaction performance of the game to a level by completing a small amount of work.
In the game, we divide sounds into two categories. The first type is music, which is usually long and suitable for environmental music (such as background music of a game ). Because of its long length, you can only play one piece of music at a time. The second type is the sound effect, which is characterized by a short length, but can play multiple sound effects at the same time, with a strong expressiveness.
Cocos2d-x provides support for music and sound effects, can be very convenient to achieve the playing, pause and loop functions of music and sound.
With sound engine game development, we can easily enable the CocosDenshion sound engine library. CocosDenshion is located in the cocosaudio directory. Typically, the Cocos2d-x project already contains the CocosDenshion library, and when we need to use the sound engine, just import the engine header. CocosDenshion implements the simple and easy-to-use SimpleAudioEngine class. To use the sound engine, we only need to introduce its header file: # include SimpleAudioEngine. h
The CocosDenshion engine library is actually an encapsulation of the system audio API. Therefore, the supported audio file format is related to the platform.

Playing music and sound effects SimpleAudioEngine is a singleton class like many Cocos2d functional components. Use the following code to access its instance:
SimpleAudioEngine::getInstance();
It provides many methods related to playing music and sound effects, which are easy to use. The following describes the most common methods. Pre-loading of music and sound effects is usually a time-consuming process. To prevent the inconsistency between the actual playback and the game caused by the delay caused by real-time loading, before playing sound effects and background music, you need to pre-load the music file. Generally, we call the following two methods in the loading phase before entering the game scenario.
Void preloadEffect (const char * pszFilePath): Used to pre-load the audio file, where pszFilePath is the directory location of the audio file. Void preloadBackgroundMusic (const char * pszFilePath): Used to pre-load background music, where pszFilePath is the directory location of the music file.

In addition to the above several commonly used audio playback related methods, Cocos2d-x also provides other very convenient control methods and properties:
Void rewindBackgroundMusic (): replay the background music. Bool isBackgroundMusicPlaying (): returns a Boolean value indicating whether the background music is being played. Void unloadEffect (const char * pszFilePath): detaches a pre-loaded audio file to release system resources. The pszFilePath parameter indicates the path of the preloaded audio file. When you no longer use a sound file, you can call this function to release resources. However, if you use this sound effect again, the engine will load the sound file again, resulting in a large amount of time. Float EffectsVolume attribute: gets or sets the volume of the sound. The value ranges from 0.0 to 1.0. Note that the setting of this attribute affects the volume of all sound effects. Float BackgroundMusicVolume attribute: gets or sets the volume of the background music. The value ranges from 0.0 to 1.0. Similar to the EffectsVolume attribute, the setting of this attribute also affects the volume of all background music. Void end (): Call this method to release resources occupied by SimpleAudioEngine when the audio engine is no longer used.

Unique sound effect identifier: Multiple sound effects may be played at the same time. Therefore, in order to differentiate each sound effect, a number is assigned to the sound effect to be played when the playEffect method is called, that is, its unique identifier. After that, if you need to pause, resume, or stop playing the sound, it will use its unique identifier to locate the sound.
Note the following three points when using SimpleAudioEngine. 1. Before playing audio or background music, you must load the audio or background music file in advance. 2. When playing background music, if you want to switch the scene, do not need to manually stop the background music, the Cocos2d-x will automatically stop the background music of the previous scene, and play the background music in the new scene (if the new scene will play the new background music ). 3. When you exit and no longer need music, you must call the end method to release the resources occupied by the engine.
Haomeng's main friendly reminder: In life, music is so indispensable! With the companionship of music, our world becomes rich and colorful. More music, optimistic life ,,,

Related Article

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.