Cocos2d-js in-game background music and sound effects

Source: Internet
Author: User
Tags set background

The processing of audio in the game is also very important, it is divided into background music playback and sound effects. Background music is played for a long time, it takes up a lot of memory, the background music can not be played more than one time. The sound is short, it takes up less memory, and the sound performance is played at the same time. An audio engine-audioengine is provided in the COCOS2D-JS, which gives a good control over the game background music and the sound effects optimized for playback.Cocos2d-js audio files
Audio multimedia files mainly store audio data information, audio files in the process of recording the sound signal through the audio encoding into audio digital signal saved to a format file. During playback, the audio files are decoded, and the decoded signals can be turned into sound waves by means of loudspeakers and other devices. Audio files have a large amount of data during encoding, so some file formats compress the data. Audio files can be divided into: (1) lossless format, non-compressed data format, file is large, generally not suitable for mobile devices. such as WAV, AU, ape and other files. (2) lossy format, for data compression, after compression lost some data, such as MP3, WMA and other files. 1.WAV file WAV files are currently the most popular lossless compression format. WAV files are flexible in format and can store multiple types of audio data. Because of the large file size, it is not suitable for devices with small storage capacity on mobile devices. The 2.mp3 file MP3 format is now very popular, and MP3 is a lossy compression format that removes parts and insensitive parts of the human ear as much as possible. The 3.WMA file WMA format is a Microsoft published file format and is also lossy compression format, which is pitches with the MP3 format. In the case of low bit rate rendering, the WMA format shows more advantages than MP3, compression is higher than MP3, and better sound quality. 4.CAFF file Caff file is Apple developed specifically for Mac OS X and iOS system uncompressed audio format. It is designed to replace the old WAV format. 5.AIFF file Aiff file is a professional audio file format developed by Apple. The compressed format of AIFF is Aiff-c, which compresses the data 4:1 compression rate and is specifically applied to Mac OS X and iOS systems. The 6.MID file mid file is a MIDI format, professional audio file format that allows digital synthesizers and other devices to exchange data. Mid files are mainly used for original instrument works, amateur performances of pop songs, game tracks, and electronic greeting cards. 7.OGG file ogg file Full name Oggvobis, is a new audio compression format, similar to MP3 and other music formats, OGG is completely free, development and no patent restrictions. The Ogg file format can be continuously improved in size and sound quality without affecting the old encoder or player.Cocos2d-js Cross-platform audio supportUnlike Cocos2d-iphone, Cocos2d-js is a game engine designed for cross-platform, which can be run not only by Cocos2d-x JSB on a mobile platform, but also by cocos2d-html in a web platform, cocos2d-x JSB is a music and sound file that local technology cocos2d-x can support. Cocos2d-js is also supported by COCOS2D-JS for background music playback for each platform format as follows: (1) Android platform support is the same as the format supported by Android.media,mediaplayer. Andriod.media,mediaplayer is an Android multimedia playback class. (2) iOS platform support recommended using MP3 and Caff format (3) The Windows platform supports MIDI, WAV and MP3 formats (4) The Windows Phone8 platform supports MIDI and WAV formats (5) The web platform depends on the specific browser, If HTML5 is supported, the OGG and MP3 formats are generally supported, which can be MP4 and m4a for pure audio, but should be tested before use. COCOS2D-JS supports the following for sound playback of various platform formats: (1) The Android platform supports Ogg and WAV files, but preferably the Ogg file (2) supported in iOS platform recommended using Caff Format (3) The Windows platform supports MIDI, WAV (4) The Windows Phone8 platform supports MIDI and WAV formats (5) The Web platform relies on a specific browser, and if supported by HTML5, the WAV format is generally supported, but needs to be tested before use.using the Audioengine engineThe COCOS2D-JS provides an audio Audioengine engine. The specific API used is Cc.AudioEngine.cc.AudioEngine has several commonly used functions: (1) playmusic (url,loop) play background music, parameter URL is the path to play the file, the parameter loop control whether the loop playback, False (2) Stopmusic () stops playing background music by default (3) Pausemusic () pauses playback of background music (4) Resumemusic () Continue playing background music (5) ismusicplaying () Determine if the background music is playing (6) Playeffect (Url,loop) plays the sound, the parameters with the Playmusic function (7) pausealleffects pause all playback effects. The parameter audioid is the Playeffect function return ID (8) pausealleffects () pauses all playing effects (9) Resumeeffect (audioid) to continue playing the sound effects, The parameter audioid is the Playeffect function return ID (TEN) resumealleffects () continues to play all sound effects (one) Stopeffect (audioid) stop playing the sound, Parameter audioid is the Playeffect function return ID (All) stopalleffects () stops all playback effectspreprocessing of audio filesIt is necessary to pre-process both the background music and the sound before playback. If you do not preprocess, you will find that the first time you play this audio file feel very "card", the user experience is not good. The preprocessing capabilities of the resource files are provided in the COCOS2D-JS. Cc. Loaderscene.preload (G_resource,funcion () {Cc.director.runScene (New Helloworldscene ());},this);}; Cc. The Loaderscene.preload function can preprocess some resources; g_resources is a resource file collection variable, which is defined in the Resource.js file. The contents of the Resource.js file are as follows:
With regard to playing background music, it is theoretically possible to place the playback code cc.audioEngine.playMusic (res.bgmusicsynth_mp3,true) in three locations (1th in the Code, 2, 3 lines) 1. Put the code in line 1th to line 1th, and if you don't call the background music Stop statement in the previous scene, you can play the background music normally. However, if the previous scene layer Helloworldlayeronexit function has a call to the background music stop statement, then the background music playback will stop for a few seconds. Note: This issue occurs regardless of whether the same file is played and stopped. 2. Put the code on line 2nd to line 2nd (that is, in Settinglayer onEnter), if the background music stop statement is not called in the previous scene, the background music can play normally, if the previous scene layer Helloworldlayeronexit function has background stop music , there will also be a phenomenon that the background music stops after a few seconds of playback. 3. Put the code on line 3rd to put the recommended code to the 3rd line of code location, because the Onentertransitiondidfinish function is in the entry layer and the transition animation at the end of the call, the code here does not have to consider whether the previous scene call background music stop music statement. And there will be no appearance for the first to hear the sound, after the appearance of the interface phenomenon. In summary, whether the background music can be successfully played with the previous scene whether the call background music Stop statement related. It is also related to which function the playback code in the current scene is in. If the previous scene does not call the background music Stop statement, the problem is simple. The playback code can be placed anywhere in the code, but if the previous scene calls the background music stop statement, the Onentertransitiondidfinish function plays the background concert better.Stop playing background musicWhere does stopping the background music playback code fit?
About stopping background music playback, theoretically it is possible to stop the playback code cc.audioengine,stopmusic (Res.bgmusicsynth_mp3) Place it in two locations (lines 1th and 2 in the code) 1. Put the code on line 1th to line 1th, if you call background music playback in the later scene, it may cause the background music to be played, but if you play the background music in the Onentertransitiondidfinish function of the later scene, it is not good to have an exception. 2. Put the code on line 2nd into line 2nd, which is the Helloworldlayer onexittransitiondidstart function. This function is executed first, if the Stop playback code is placed here, will not affect the background music playback of other scenes, it is recommended to stop the playback code here. Instance:Set background music and sound effectsFirst step: Re-Modify the Resource.js file. Step Two: Implement


The 1th line of code var audioengine= Cc.audioengine declares and initializes the global variable audioengine, because Cc.audioengine uses a singleton design, Audioengine saves the Cc.audioengine singleton object. The 2nd line of code Var Iseffectplay=true declares that the global variable iseffectplay.iseffectplay indicates whether the sound can play. Code 3,4,5 Line Audioengine (res.effectblip_wav) is the sound effect when the stand-alone menu is played. Code 6th, Line 7 Audioengine.playmusic (res.bgmusicsynth_mp3,true) play background music

Cocos2d-js in-game background music and sound effects

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.