Cocos2d-JS using CocosDenshion Engine
The Cocos2d-JS provides an audio CocosDenshion engine. The specific API used is cc. AudioEngine. Cc. AudioEngine has several common functions:
PlayMusic (url, loop ). Play background music. The url parameter is the path of the playback file. The loop parameter controls whether to play the video cyclically. The default value is false.
StopMusic (). Stop playing background music.
PauseMusic (). Pause playing background music.
ResumeMusic (). Continue playing background music.
IsMusicPlaying (). Determine whether the background music is being played.
PlayEffect (url, loop ). Play Sound Effects. parameters are the same as playMusic functions.
PauseEffect (audioID ). Pause the playback effect. The audioID parameter is the ID returned by the playEffect function.
PauseAllEffects (). Pause all playing sound effects.
ResumeEffect (audioID ). Play the audio effect again. The parameter audioID is the ID returned by the playEffect function.
ResumeAllEffects (). Continue to play all audio effects.
StopEffect (audioID ). Stop playing audio. The parameter audioID is the ID returned by the playEffect function.
StopAllEffects (). Stop all playing sound effects.
Audio File preprocessing
It is necessary to pre-process Background Music or sound effects before playing. If preprocessing is not performed, you will find that the first time you play the audio file, it feels "stuck" and the user experience is poor. Resource file preprocessing is provided in the Cocos2d-JS.
There is a main. js in the Cocos2d-JS project generated by the template, its content is as follows:
cc.game.onStart = function(){ cc.view.setDesignResolutionSize(1136, 640, cc.ResolutionPolicy.EXACT_FIT);cc.view.resizeWithBrowserSize(true); //load resources cc.LoaderScene.preload(g_resources, function () {① cc.director.runScene(new HelloWorldScene()); }, this);};cc.game.run();The cc. LoaderScene. preload function can pre-process some resources. g_resources is the collection variable of the resource file, which is defined in the resource. js file. The content of the resource. js file is as follows:
var res = { //image On_png: res/on.png, Off_png: res/off.png, background_png: res/background.png, start_up_png: res/start-up.png, start_down_png: res/start-down.png, setting_up_png: res/setting-up.png, setting_down_png: res/setting-down.png, help_up_png: res/help-up.png, help_down_png: res/help-down.png, setting_back_png: res/setting-back.png, ok_down_png: res/ok-down.png, ok_up_png: res/ok-up.png, //plist //fnt //tmx //bgm //music bgMusicSynth_mp3: 'res/sound/Synth.mp3', ① bgMusicJazz_mp3: 'res/sound/Jazz.mp3'② //effect};var g_resources = [③];for (var i in res) {④ g_resources.push(res[i]);}
Line ③ of the above Code defines the resource set variable g_resources. The for loop in line ④ is to add the background music resource file to the g_resources resource set variable. Note: To prevent hard encoding, we need to add the resource alias declaration to the res variable. For details, see line ① and line ② of the Code.
The preceding settings allow the game application to load all resource files, including images, sounds, property list files (plist), font files (fnt), and tile Map Files (tmx) when running).
Play background music
The example code of playing and stopping background music is as follows:
cc.audioEngine.playMusic(res.bgMusicSynth_mp3, true);cc.audioEngine.stopMusic(res.bgMusicSynth_mp3);
Cc. audioEngine is the object created by the cc. AudioEngine class.
Where is the playing code of background music suitable? For example, in the Setting scenario, the main code is as follows:
Var SettingLayer = cc. layer. extend ({ctor: function () {this. _ super (); cc. log (SettingLayer init); // playback code ① return true;}, onEnter: function () {this. _ super (); cc. log (SettingLayer onEnter); // playback code ②}, onEnterTransitionDidFinish: function () {this. _ super (); cc. log (SettingLayer onEnterTransitionDidFinish); // playback code ③}, onExit: function () {this. _ super (); cc. log (SettingLayer onExit); // playback code ④}, onExitTransitionDidStart: function () {this. _ super (); // playback code ⑤ }});For playing background music, we can theoretically place the playing code cc. audioEngine. playMusic (res. bgMusicSynth_mp3, true) in three places (①, ②, ③ In the code ). Next we will analyze their differences separately.
1. Place the Code in line ①.
Put the Code in line ① (that is, in the ctor constructor). If the background music stop statement is not called in the previous scenario, the background music can be played normally. However, if the preceding scene layer HelloWorldLayer onExit function calls the background music stop statement, the background music will be played several seconds and then stopped.
To explain this phenomenon, we can refer to the related content of switching the lifecycle of multiple scenarios. Use the pushScene function to enter the Setting scenario from the HelloWorld implementation scenario. The sequence of Life Cycle Function calls is shown in.
The sequence of lifecycle events is visible. The HelloWorldLayer onExit call is after SettingLayer init (ctor constructor), so when we start playing background music in SettingLayer init, after a while, call HelloWorldLayer onExit to stop playing background music.
Note that a problem may occur regardless of whether the player is playing or stopped on the same file.
2. Place the Code in line ②.
Put the Code in line ② (that is, in the SettingLayer onEnter function). If the background music stop statement is not called in the previous scenario, the background music can be played normally. If the preceding scene layer HelloWorldLayer onExit function has a background music stop statement, the background music will be played several seconds and then stopped. The reason is the same as that in line ① Of the code.
3. Place the Code in line ③
We recommend that you place the Code in line ③, because the onEnterTransitionDidFinish function is called at the entry layer and the end of the transition animation. If the code is put here, you do not need to consider whether the previous scenario has called the background music stop statement. In addition, the user will not hear the sound first, and then the interface will appear.
To sum up, whether the background music can be played successfully is related to the function in which the background music stop statement is called in the previous scenario. If the previous scenario does not call the stop Statement of background music, the problem will be simple. We can place the playback code in code ①, ②, or ③. However, if the background music stop statement is called in the previous scenario, it is better to play the background music in the onEnterTransitionDidFinish function.
Stop playing background music
Where can I choose to stop playing background music? For example, in the HelloWorld scenario, the main code is as follows:
Var HelloWorldLayer = cc. layer. extend ({ctor: function () {this. _ super (); cc. log (HelloWorldLayer init);}, onEnter: function () {this. _ super (); cc. log (HelloWorldLayer onEnter) ;}, onEnterTransitionDidFinish: function () {this. _ super (); cc. log (HelloWorldLayer onEnterTransitionDidFinish);}, onExit: function () {this. _ super (); cc. log (HelloWorldLayer onExit); // stop the playback code ①}, onExitTransitionDidStart: function () {this. _ super (); // stop the playback code ② }});In theory, we can put the stop code cc. audioEngine. stopMusic (res. bgMusicSynth_mp3) in two places (① and ② In the code ). Next we will analyze their differences separately.
1. Place the Code in line ①.
Put the Code in line ① (that is, in the HelloWorldLayer onExit function). If you call the background music function in a later scenario, playing background music may be abnormal, however, if you play background music in the onEnterTransitionDidFinish function in the following scenario, there will be no exception. We have discussed this issue in the previous section.
2. Place the Code in line ②.
Put the code in row ② (that is, the HelloWorldLayer onExitTransitionDidStart function). As shown in Figure 9-1, the first HelloWorldLayer onExitTransitionDidStart function is executed. If our stop playback code is put here, it does not affect the playing of background music in other scenarios. We recommend that you put the Stop playback code here.