In the game, we divide the music files into two categories. One is the background music, the second is the sound. For example, into a scene, there will be the sound of background music, the protagonist is to make the action, there will be sound production and so on.
However, background music and sound music are not the same, in general, the background music is very long, but the sound is very short, only a few seconds just. So, the background music can only play a song, however, the sound can be played at the same time, I believe we play the game also have some experience.
In cocos2d-x3.x, we can easily call the Cocosdenshion Sound engine library. When we need to load the audio file, we just have to write the relevant header file in it.
Here's a look at the audio files supported by this library.
Android:mp3, Mid, Ogg, and WAV. The difference between these formats can go to the Niang view.
IOS:AAC, CAF, MP3, M4A, and WAV.
Windows:mid, MP3, and WAV.
Simpleaudioengine, like many cocos2d parts, is a singleton class. We can use the following code to visit its instance:
Simpleaudioengine::shareengine ();
Loading music and sound effects files is often a time-consuming task. To prevent the delay caused by loading the file from causing the actual play to be uncoordinated with the game, pre-loading the music file before playing the sound and background music. In general, we will call the following method in the load phase before entering the game scene.
void Preloadeffect (const char* Pszfilepath): Used to preload a sound file, where Pszfilepath is the directory location where the sound file resides. void Preloadbackgroundmusic (const char* Pszfilepath): Used to preload background music where Pszfilepath is the location of the directory where the music files reside.
Play and stop:
The audio engine provides a very convenient interface for playing and pausing music sounds, which is very convenient to use. The API is as follows:
unsigned int playeffect (const char* Pszfilepath, BOOL bloop = False)
Play the sound. Is the same as the Preload method, the previous parameter is the path, the latter is the loop to play, and the default is False, which can only play once. In general, the sound does not need to be looped and the background music needs to be looped back
。 As follows:
Simpleaudioengine::sharedengine ()->playeffect ("Coin.wav", false);
In addition, the Playeffect function returns an unsigned number, which is the only indication of the audio playback. Can be used to pause the sound.
void Playbackgroundmusic (const char* Pszfilepath, BOOL bloop = false);
Play the background music, the parameters and the previous is the same, there is not much to say.
void Stopeffect (unsigned int nsoundid);
Stops playing the specified sound, and the parameter inside is the one that returns the sound.
void Stopalleffects (); Stop playing all sound effects. void Stopbackgroundmusic (); Stop playing background music.
Pause and Resume:
When the game enters the background, the music is usually paused, and the music continues to play when the game returns to the foreground.
void Pausebackgroundmusic ();
Pauses the playback of the background music. After pausing, you can resume playback by using the Restore music playback method.
void Pauseeffect (unsigned int nsoundid);
Pauses playback of the specified sound, and the parameter is the only indication of the sound that will be paused.
void Pausealleffects (); Pauses all sound effects in playback.
<span style= "font-family:arial, Helvetica, Sans-serif;" >void Resumebackgroundmusic ();</span>
Resumes playback of the background music and calls this method when the game re-enters the foreground. We can call this method in the event handler function provided by Appdelegate to recover the paused music.
void Resumeeffect (unsigned int nsoundid);
To restore a paused sound, the parameter is the only indication of the sound.
void Rewindbackgroundmusic (); re-play the background music.
BOOL Isbackgroundmusicplaying (): Returns a Boolean value that indicates whether the background music is playing.
void Unloadeffect (const Char*pszfilepath): Unloads the pre-loaded sound effects file to free up system resources.
You can call this method to unload a resource when you are no longer using it, but it takes a lot of time to reload the resource if you want to use it again.
Float Effectsvolume Property: Gets or sets the volume size of the sound effect, which is a floating-point number from 0.0 to 1.0. Note that setting this property affects the volume size of all sound effects. Float Backgroundmusicvolume Property: Gets or sets the volume size of the background music, which is a floating-point number from 0.0 to 1.0. Similar to the Effectsvolume property, the setting for this property also affects the volume size of all background music. void End (): This method is called when the audio engine is no longer used to free the resources that simpleaudioengine consumes.
"Cocos2d-x game development" background music and sound effects