1 /**************************************************************************** 2 Copyright (c) 2011-2012 cocos2d-x.org 3 Copyright (c) 2013-2014 Chukong Technologies Inc. 4 5 http://www.cocos2d-x.org 6 7 Permission is hereby granted, free of charge, to any person obtaining a copy 8 of this software and associated documentation files (the "Software"), to deal 9 in the Software without restriction, including without limitation the rights 10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 copies of the Software, and to permit persons to whom the Software is 12 furnished to do so, subject to the following conditions: 13 14 The above copyright notice and this permission notice shall be included in 15 all copies or substantial portions of the Software. 16 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 THE SOFTWARE. 24 ****************************************************************************/ 25 26 /** 27 * The audio component for Cocostudio. 28 * @class 29 * @extends ccs.Component 30 */ 31 ccs.ComAudio = ccs.Component.extend(/** @lends ccs.ComAudio# */{ 32 _filePath: "", 33 _loop: false, 34 35 /** 36 * Construction of ccs.ComAudio 37 */ 38 ctor: function () { 39 cc.Component.prototype.ctor.call(this); 40 this._name = "Audio"; 41 }, 42 43 /** 44 * Initializes a ccs.ComAudio. 45 * @returns {boolean} 46 */ 47 init: function () { 48 return true; 49 }, 50 51 /** 52 * The callback calls when a audio component enter stage. 53 * @override 54 */ 55 onExit: function () { 56 this.stopBackgroundMusic(true); 57 this.stopAllEffects(); 58 }, 59 60 /** 61 * Stops all audios. 62 */ 63 end: function () { 64 cc.audioEngine.end(); 65 }, 66 67 /** 68 * Preload background music resource 69 * @param {String} pszFilePath 70 */ 71 preloadBackgroundMusic: function (pszFilePath) { 72 cc.loader.load(pszFilePath); 73 }, 74 75 /** 76 * Play background music 77 * @param {String} [pszFilePath] 78 * @param {Boolean} [loop] 79 */ 80 playBackgroundMusic: function (pszFilePath, loop) { 81 if(pszFilePath){ 82 cc.audioEngine.playMusic(pszFilePath, loop); 83 }else{ 84 cc.audioEngine.playMusic(this._filePath, this._loop); 85 } 86 }, 87 88 /** 89 * Stop background music 90 * @param {String} releaseData 91 */ 92 stopBackgroundMusic: function (releaseData) { 93 cc.audioEngine.stopMusic(releaseData); 94 }, 95 96 /** 97 * Pause background music 98 */ 99 pauseBackgroundMusic: function () { 100 cc.audioEngine.pauseMusic(); 101 }, 102 103 /** 104 * Resume background music 105 */ 106 resumeBackgroundMusic: function () { 107 cc.audioEngine.resumeMusic(); 108 }, 109 110 /** 111 * Rewind background music 112 */ 113 rewindBackgroundMusic: function () { 114 cc.audioEngine.rewindMusic(); 115 }, 116 117 /** 118 * Indicates whether any background music can be played or not. 119 * @returns {boolean} 120 */ 121 willPlayBackgroundMusic: function () { 122 return cc.audioEngine.willPlayMusic(); 123 }, 124 125 /** 126 * Whether the music is playing. 127 * @returns {Boolean} 128 */ 129 isBackgroundMusicPlaying: function () { 130 return cc.audioEngine.isMusicPlaying(); 131 }, 132 133 /** 134 * The volume of the music max value is 1.0,the min value is 0.0 . 135 * @returns {Number} 136 */ 137 getBackgroundMusicVolume: function () { 138 return cc.audioEngine.getMusicVolume(); 139 }, 140 141 /** 142 * Set the volume of music. 143 * @param {Number} volume must be in 0.0~1.0 . 144 */ 145 setBackgroundMusicVolume: function (volume) { 146 cc.audioEngine.setMusicVolume(volume); 147 }, 148 149 /** 150 * The volume of the effects max value is 1.0,the min value is 0.0 . 151 * @returns {Number} 152 */ 153 getEffectsVolume: function () { 154 return cc.audioEngine.getEffectsVolume(); 155 }, 156 157 /** 158 * Set the volume of sound effects. 159 * @param {Number} volume 160 */ 161 setEffectsVolume: function (volume) { 162 cc.audioEngine.setEffectsVolume(volume); 163 }, 164 165 /** 166 * Play sound effect. 167 * @param {String} [pszFilePath] 168 * @param {Boolean} [loop] 169 * @returns {Boolean} 170 */ 171 playEffect: function (pszFilePath, loop) { 172 if (pszFilePath) 173 return cc.audioEngine.playEffect(pszFilePath, loop); 174 else 175 return cc.audioEngine.playEffect(this._filePath, this._loop); 176 }, 177 178 /** 179 * Pause playing sound effect. 180 * @param {Number} soundId 181 */ 182 pauseEffect: function (soundId) { 183 cc.audioEngine.pauseEffect(soundId); 184 }, 185 186 /** 187 * Pause all effects 188 */ 189 pauseAllEffects: function () { 190 cc.audioEngine.pauseAllEffects(); 191 }, 192 193 /** 194 * Resume effect 195 * @param {Number} soundId 196 */ 197 resumeEffect: function (soundId) { 198 cc.audioEngine.resumeEffect(soundId); 199 }, 200 201 /** 202 * Resume all effects 203 */ 204 resumeAllEffects: function () { 205 cc.audioEngine.resumeAllEffects(); 206 }, 207 208 /** 209 * Stop effect 210 * @param {Number} soundId 211 */ 212 stopEffect: function (soundId) { 213 cc.audioEngine.stopEffect(soundId); 214 }, 215 216 /** 217 * stop all effects 218 */ 219 stopAllEffects: function () { 220 cc.audioEngine.stopAllEffects(); 221 }, 222 223 /** 224 * Preload effect 225 * @param {String} pszFilePath 226 */ 227 preloadEffect: function (pszFilePath) { 228 cc.loader.getRes(pszFilePath); 229 this.setFile(pszFilePath); 230 this.setLoop(false); 231 }, 232 233 /** 234 * Unload effect 235 * @param {String} pszFilePath 236 */ 237 unloadEffect: function (pszFilePath) { 238 cc.audioEngine.unloadEffect(pszFilePath); 239 }, 240 241 /** 242 * File path setter 243 * @param {String} pszFilePath 244 */ 245 setFile: function (pszFilePath) { 246 this._filePath = pszFilePath; 247 }, 248 249 /** 250 * Sets audio component whether plays loop 251 * @param {Boolean} loop 252 */ 253 setLoop: function (loop) { 254 this._loop = loop; 255 }, 256 257 /** 258 * Returns the file path of audio component. 259 * @returns {string} 260 */ 261 getFile: function () { 262 return this._filePath; 263 }, 264 265 /** 266 * Returns audio component whether plays loop 267 * @returns {boolean} 268 */ 269 isLoop: function () { 270 return this._loop; 271 } 272 }); 273 274 /** 275 * allocates and initializes a ComAudio. 276 * @deprecated since v3.0, please use new construction instead. 277 * @return {ccs.ComAudio} 278 * @example 279 * // example 280 * var com = ccs.ComAudio.create(); 281 */ 282 ccs.ComAudio.create = function () { 283 var com = new ccs.ComAudio(); 284 if (com && com.init()) 285 return com; 286 return null; 287 };